Bing Maps进阶系列四:路由功能服务(RouteService)

本文介绍了如何使用BingMaps的路由服务实现从一个城市到另一个城市的路径计算,并展示了如何在地图上绘制路径及标记途经点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  Bing Maps提供的路由功能服务(RouteService)可以实现多方位的计算地图上的路线指示,路径行程等功能,比如说实现驾驶路线和地点,旅游航线等。可以通过该服务的地址(http://dev.virtualearth.net/WebServices/v1/RouteService/RouteService.svc)添加该服务的Web服务引用。

        

  路由功能服务提供了两个方法,分别是CalculateRoute()和CalculateRoutesFormMajorRoads(),其实现的功能分别如下:

  1、CalculateRoute:计算路径并返回指定的路径行程,以及路径的其他相关路径数据(包括道路名称,行程方法等等)。

  2、CalculateRoutesFormMajorRoads:计算指定地理位置点或路径方向,以及附近的道路信息。

  为了更加清楚的介绍路由功能服务的接口使用方法,下面将通过一个实例来介绍路由服务接口的详细应用。需要实现从成都到重庆的路由检索,然后标记处从成都到重庆的路程行程图和中途经度地市的行程点。

  通过前面对路由功能服务(RouteService)添加了服务引用,Silverlight客户端就会自动生成该服务的客户端代理对象,在实现上面所提出的实例需求前先对RouteServiceClient进行一下封装,并指定了调用接口完成后事件处理函数:client_CalculateRouteCompleted。如下代码块:

<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--> private RouteServiceClientclient;
public
RouteServiceClientClient
{
get

{
if(client==null
)
{
booluriScheme=!Application.Current.IsRunningOutOfBrowser&&
HtmlPage.Document.DocumentUri.Scheme.Equals(Uri.UriSchemeHttps);
BasicHttpBindingbinding
=newBasicHttpBinding(uriScheme?
BasicHttpSecurityMode.Transport:BasicHttpSecurityMode.None);
binding.MaxReceivedMessageSize
=int
.MaxValue;
binding.MaxBufferSize
=int
.MaxValue;
UriBuilderuri
=newUriBuilder("http://dev.virtualearth.net/webservices/v1/RouteService/RouteService.svc"
);

if
(uriScheme)
{
uri.Scheme
=
Uri.UriSchemeHttps;
uri.Port
=-1
;
}

client
=newRouteServiceClient(binding,new
EndpointAddress(uri.Uri));
//调用完成后的事件处理函数

client.CalculateRouteCompleted+=newEventHandler<CalculateRouteCompletedEventArgs> (client_CalculateRouteCompleted);
}
return
client;
}
}

  路由检索接口的调用非常简单,要想检索出那几个地点之间的路径行程只需要将地点所对应地理位置坐标(经度和纬度)传递给路由请求对象就可以了。详细如下代码块:

<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--> privatevoidbtnCalculateRoute_Click(object sender,RoutedEventArgse)
{
varrequest
=new
RouteRequest();
request.Credentials
=new
Credentials();
request.Credentials.ApplicationId
="AkzZURoD0H2Sle6Nq_DE7pm7F3xOc8S3CjDTGNWkz1EFlJJkcwDKT1KcNcmYVINU"
;


//设置开始、结束坐标点

varwaypoints=newSystem.Collections.ObjectModel.ObservableCollection<Waypoint>
{
newWaypoint(){Description="成都",Location=newLocation(){Latitude=30.6666587469201,Longitude=104.062021177233 }},
newWaypoint(){Description="重庆",Location=newLocation(){Latitude=29.5076372217973,Longitude=106.489384971208
}},
};
request.Waypoints
=
waypoints;
request.ExecutionOptions
=new
ExecutionOptions();
request.ExecutionOptions.SuppressFaults
=true
;
request.Options
=new
RouteOptions();
request.Options.RoutePathType
=
RoutePathType.Points;

Client.CalculateRouteAsync(request);
}

  如上代码块中定义,通过构造好了成都和重庆的两点地理坐标给路由请求对象(RouteRequest)的路径途经点(Waypoint),然后调用路由功能服务接口方法实现路径行程线检索。最终接口的返回值就交给了上面RouteServiceClient中封装过程中指定的结果处理事件函数去处理了,这里需要分析清楚几点。

  1、在接口请求完成事件处理函数里能够获取到请求响应结果(RouteResponse),包括状态,规则,路径点等一系列结果参数。

  2、可以通过规则检测或获取两点或多个路程点之间的路径点数量。

  3、知道路径点(既经过点的地理位置坐标),可以通过在地图上绘制不估计值线条表示出路线图。

  4、可以在路径点上通过某种特殊的符号标记为路径是通过了该路径点的,比如用圆形、三角形或者一张有特殊意义的图片等。

  5、总结上面四点得出结论,通过调用路径检索接口得到路径点坐标后,通过在地图上绘制路径线路图,并在路径点上通过圆形标记。

  通过上面的分析,几乎已经将下面要做的事情用汉语描述清楚了,下面代码块则是实现上面五点的程序代码:

<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--> privatevoidclient_CalculateRouteCompleted(object sender,CalculateRouteCompletedEventArgse)
{
RouteResponserouteResponse
=
e.Result;
try

{
if(e.Result.ResponseSummary.StatusCode!= ResponseStatusCode.Success)
{
MessageBox.Show(
"错误的路由状态"
);
}
elseif(e.Result.Result.Legs.Count==0
)
{
MessageBox.Show(
"没找到路由规则"
);
}
else

{
//初始化路线线路点坐标及其他属性
MapPolylineline=new MapPolyline();
line.Locations
=new
LocationCollection();
line.Stroke
=new
SolidColorBrush(Colors.Blue);
line.Opacity
=0.66
;
line.StrokeThickness
=5
;

foreach(varpointin
e.Result.Result.RoutePath.Points)
{
line.Locations.Add(
new
Location(point.Latitude,point.Longitude));
}
RouteLayer.Children.Add(line);

//记录开始点和结束点

LocationRectrect=newLocationRect(line.Locations[0],line.Locations[line.Locations.Count-1 ]);

//遍历处理服务调用接口中的路程行程点,将各个行程点上绘制一个红色圆形标记

foreach(varitemine.Result.Result.Legs[0 ].Itinerary)
{
Ellipseellipse
=newEllipse(){Width=10,Height=10,Fill=newSolidColorBrush(Colors.Red),Opacity=0.66,Tag=
item};
Locationlocation
=new
Location(item.Location.Latitude,item.Location.Longitude);
MapLayer.SetPosition(ellipse,location);
MapLayer.SetPositionOrigin(ellipse,PositionOrigin.Center);

RouteLayer.Children.Add(ellipse);
}
map.SetView(rect);
}
}
catch
(Exception)
{
MessageBox.Show(
"路由解析过程中出现异常"
);
}
}

程序代码里使用到的RouteLayer是在地图控件里加入了一个MapLaye图层,r专门用来呈现的路径行程图和路径标记,直接使用Bing Maps Silverlight Control提供的MapLayer,如下代码块:

<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--> <m:MapCredentialsProvider="BingMaps开发Key"x:Name="map">
<m:MapLayerx:Name="RouteLayer"></m:MapLayer>
</m:Map>

  编译运行最终的效果如下图所示:

        

  如果您想了解更多关于Bing Maps地图服务的知识,请查询:

  MSDN:http://msdn.microsoft.com/en-us/library/cc980922.aspx

  Bing Maps开发站:http://www.microsoft.com/maps/developers/

  Bing Maps开发SDK:http://msdn.microsoft.com/en-us/library/dd877180.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值