在UWP中使用Bing Map SDK获取所有路线

时间:2016-05-16 17:02:51

标签: bing-maps uwp-xaml uwp-maps

是否可以使用Bing Map SDK在UWP中获取指定FROM和TO位置的所有行车路线? (就像windows 10 map app)

1 个答案:

答案 0 :(得分:1)

是: 通过调用MapRouteFinder类的方法获取驾驶或步行路线和路线 - 例如,GetDrivingRouteAsync或GetWalkingRouteAsync。 MapRouteFinderResult对象包含您通过其Route属性访问的MapRoute对象。

当您请求路线时,您可以指定以下内容: •您只能提供起点和终点,或者您可以提供一系列路点来计算路线。 •您可以指定优化 - 例如,最小化距离。 •您可以指定限制 - 例如,避开高速公路。

您可以使用以下示例代码:

    private async void GetRouteAndDirections()
    {
        // Start at Microsoft in Redmond, Washington.
        BasicGeoposition startLocation = new BasicGeoposition();
        startLocation.Latitude = 47.643;
        startLocation.Longitude = -122.131;
        Geopoint startPoint = new Geopoint(startLocation);

        // End at the city of Seattle, Washington.
        BasicGeoposition endLocation = new BasicGeoposition();
        endLocation.Latitude = 47.604;
        endLocation.Longitude = -122.329;
        Geopoint endPoint = new Geopoint(endLocation);

        // Get the route between the points.
        MapRouteFinderResult routeResult =
            await MapRouteFinder.GetDrivingRouteAsync(
            startPoint,
            endPoint,
            MapRouteOptimization.Time,
            MapRouteRestrictions.None);

        if (routeResult.Status == MapRouteFinderStatus.Success)
        {
            // Display summary info about the route.
            tbOutputText.Inlines.Add(new Run()
            {
                Text = "Total estimated time (minutes) = "
                    + routeResult.Route.EstimatedDuration.TotalMinutes.ToString()
            });
            tbOutputText.Inlines.Add(new LineBreak());
            tbOutputText.Inlines.Add(new Run()
            {
                Text = "Total length (kilometers) = "
                    + (routeResult.Route.LengthInMeters / 1000).ToString()
            });
            tbOutputText.Inlines.Add(new LineBreak());
            tbOutputText.Inlines.Add(new LineBreak());

            // Display the directions.
            tbOutputText.Inlines.Add(new Run()
            {
                Text = "DIRECTIONS"
            });
            tbOutputText.Inlines.Add(new LineBreak());

            foreach (MapRouteLeg leg in routeResult.Route.Legs)
            {
                foreach (MapRouteManeuver maneuver in leg.Maneuvers)
                {
                    tbOutputText.Inlines.Add(new Run()
                    {
                        Text = maneuver.InstructionText
                    });
                    tbOutputText.Inlines.Add(new LineBreak());
                }
            }
        }
        else
        {
            tbOutputText.Text =
                "A problem occurred: " + routeResult.Status.ToString();
        }

    }

此处有更多信息:https://msdn.microsoft.com/en-us/library/windows/apps/xaml/dn631250.aspx#getting_a_route_and_directions