在 Windows 8 应用中使用 C#/XAML 通过 Bing 地图绘制驾驶路线






4.87/5 (14投票s)
如何在 Windows 8 应用中使用 C#/XAML 通过 Bing 地图绘制驾驶路线。
介绍
本文档指导您如何在 C#/XAML Metro 应用中使用 Bing 地图绘制从一个地点到另一个地点的路径。它基本上使用 Bing 地图的 REST 服务,并使用辅助类进行 JSON 解析。
背景
在我的一个 Windows 8 应用中,我需要提供一个功能,使用 Bing 地图绘制从一个地点到另一个地点的路线。我在 Google 上搜索了很多,最终在 JavaScript 中找到一个示例,但我希望它使用 C#/XAML。因此,我向 MSDN 论坛提问,Richard Brundritt 通过提供 Bing 地图 REST 服务辅助类来帮助我。我利用它完成了这项工作。在这里我发布代码。所有 REST 链接和 URL 模板都可以在 这里 找到。感谢 Richard Brundritt
先决条件
要在您的 Windows 8 应用中使用 Bing 地图,您需要 Bing 地图 SDK 和用于使用它们的密钥。因此,这里有一个 CodeProject 文章 展示了基本的 Bing 地图操作。然后添加 Bing 地图 REST 服务 .NET 库。请注意,Bing 地图在某些地区不受支持。您可以在 这里 检查。当不支持时,您将在 Bing 地图上看到 红色叉号。因此,要设置受支持的区域,您必须设置 HomeRegion
属性,否则它将使用 Windows 8 上的用户区域。
使用代码
此应用使用 Bing 地图的 REST 服务。因此,它首先生成一个线程以获取异步响应。该线程将获取 REST 服务的 URL,该 URL 可以将两个位置的坐标或地标作为参数,并返回一个 JSON 字符串。DataContractJsonSerializer
类用于序列化 JSON 响应。
private async Task<Response> GetResponse(Uri uri)
{
System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
var response = await client.GetAsync(uri);
using (var stream = await response.Content.ReadAsStreamAsync())
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Response));
return ser.ReadObject(stream) as Response;
}
}
现在,当用户启动时,用户将能够传递用于查找路线的参数。用户可以传递坐标或位置,据此在地图上绘制路线。我传递了一个从艾哈迈达巴德到孟买的路线值。这里 MyMap
是 XAML 中的 Map
控件,MyPushPin
是起始位置的图钉。MapShapeLayer
类用于在地图上创建图层以绘制折线或多边形。
private async void btnGiveDirections_Click(object sender, RoutedEventArgs e)
{
try
{
tbError.Text = string.Empty;
if (rdCoord.IsChecked == true)
URL = "http://dev.virtualearth.net/REST/V1/Routes/Driving?o=json&wp.0=" +
txtFromCoord.Text + "&wp.1=" + txtToCoord.Text +
"&optmz=distance&rpo=Points&key=" + MyMap.Credentials;
else
URL = "http://dev.virtualearth.net/REST/V1/Routes/Driving?o=json&wp.0=" +
txtFromLocation.Text + "&wp.1=" + txtToLocation.Text +
"&optmz=distance&rpo=Points&key=" + MyMap.Credentials;
Uri geocodeRequest = new Uri(URL);
BingMapsRESTService.Response r = await GetResponse(geocodeRequest);
geolocator = new Geolocator();
MyPushPin = new Pushpin();
FromLatitude = ((BingMapsRESTService.Route)(r.ResourceSets[0].Resources[0])).RoutePath.Line.Coordinates[0][0];
FromLongitude = ((BingMapsRESTService.Route)(r.ResourceSets[0].Resources[0])).RoutePath.Line.Coordinates[0][1];
location = new Location(FromLatitude, FromLongitude);
MapLayer.SetPosition(MyPushPin, location);
MyMap.Children.Add(MyPushPin);
MyMap.SetView(location, 15.0f);
MapPolyline routeLine = new MapPolyline();
routeLine.Locations = new LocationCollection();
routeLine.Color = Windows.UI.Colors.Blue;
routeLine.Width = 5.0;
// Retrieve the route points that define the shape of the route.
int bound = ((BingMapsRESTService.Route)
(r.ResourceSets[0].Resources[0])).RoutePath.Line.Coordinates.GetUpperBound(0);
for (int i = 0; i < bound; i++)
{
routeLine.Locations.Add(new Location
{
Latitude = ((BingMapsRESTService.Route)
(r.ResourceSets[0].Resources[0])).RoutePath.Line.Coordinates[i][0],
Longitude = ((BingMapsRESTService.Route)
(r.ResourceSets[0].Resources[0])).RoutePath.Line.Coordinates[i][1]
});
}
MapShapeLayer shapeLayer = new MapShapeLayer();
shapeLayer.Shapes.Add(routeLine);
MyMap.ShapeLayers.Add(shapeLayer);
}
catch (Exception)
{
tbError.Text = "Error Occured !!! Please Try Again";
}
}
关注点
因此,此演示展示了开发人员如何利用 Bing 地图的 REST 服务进行路线计算和绘制到地图上。