在 Map Wpf 上绘制路线
Draw route on Map Wpf
我有地图控制:
<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].
});
所以我的 "wholeRoute" 变量是 RouteLeg[] 类型。如何在我的 bing 地图控件上显示该路线?
我是这样做的:
只需解析坐标,创建 LocationCollection 并将其应用于 MapPolyLine。最后一部分是将 MapPolyLine 添加到 bing 地图控制器。
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);
我有地图控制:
<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].
});
所以我的 "wholeRoute" 变量是 RouteLeg[] 类型。如何在我的 bing 地图控件上显示该路线?
我是这样做的:
只需解析坐标,创建 LocationCollection 并将其应用于 MapPolyLine。最后一部分是将 MapPolyLine 添加到 bing 地图控制器。
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);