在地图Wpf上绘制路线

时间:2018-03-30 19:47:33

标签: c# wpf bing-maps

我有地图控制权:

<wpf:Map x:Name="EventMapBing" CredentialsProvider="MY API" MouseDoubleClick="EventMapBing_MouseDoubleClick">

我知道如何获得整条路线:

private void GetResponse(Uri uri, Action<Response> callback)
{
    WebClient wc = new WebClient();
    wc.OpenReadCompleted += (o, a) =>
    {
        if (callback != null)
        {
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Response));
            callback(ser.ReadObject(a.Result) as Response);
        }
    };
    wc.OpenReadAsync(uri);
}

获取数据:

 string from = "gdansk";
            string to = "sopot";
            string query =
                string.Format(
                    "https://dev.virtualearth.net/REST/V1/Routes/Driving?wp.0={0}&wp.1={1}&optmz=distance&routeAttributes=routePath&key=MYAPI",
                    from, to);
            Uri geocodeRequest = new Uri(query);

            GetResponse(geocodeRequest, (x) =>
            {
                var route = x.ResourceSets[0].Resources[0] as Route;
                var wholeRoute = route.RouteLegs;
                //  x.ResourceSets[0].Resources[0].
            });

所以我的&#34;整个路线&#34;变量是RouteLeg []类型。如何在我的bing地图控件上显示该路线?

1 个答案:

答案 0 :(得分:0)

我就是这样做的:

只需解析坐标,创建LocationCollection并将其应用于MapPolyLine。最后一部分是将MapPolyLine添加到bing map controller。

  Route route = r.ResourceSets[0].Resources[0] as Route;

                        double[][] routePath = route.RoutePath.Line.Coordinates;
                        LocationCollection locs = new LocationCollection();

                        for (int i = 0; i < routePath.Length; i++)
                        {
                            if (routePath[i].Length >= 2)
                            {
                                locs.Add(new Microsoft.Maps.MapControl.WPF.Location(routePath[i][0], routePath[i][1]));
                            }
                        }

                        MapPolyline routeLine = new MapPolyline()
                        {
                            Locations = locs,
                            Stroke = new SolidColorBrush(Colors.Blue),
                            StrokeThickness = 5
                        };

                        MyMap.Children.Add(routeLine);