"HorizontalAlignment="Left"VerticalAlignment="Center"FontSize="28">
21
22
23
24
25
26
复制代码
后台处理程序,代码比较长:
1publicpartialclassMainPage:
PhoneApplicationPage
2{
3//构造函数
4publicMainPage()
5{
6InitializeComponent();
7}
8
9///
10 ///查询路线
11 ///
12 ///
13 ///
14privatevoidbtnQueryPath_Click(objectsender,RoutedEventArgse)
15{
16geocodeResults=newGeocodeResult[2];
17
18GetGeocodeAsync(this.txtStartPath.Text,0);//起点
19GetGeocodeAsync(this.txtStopPath.Text,1);//终点
20}
21
22///
23 ///根据地名查询地理坐标
24 ///
25 ///
26publicvoidGetGeocodeAsync(stringstrAddress,intwaypointIndex)
27{
28try
29{
30//实例化GeocodeService客户端对象
31GeocodeServiceClientclient=newGeocodeServiceClient("BasicHttpBinding_IGeocodeService");
32//绑定事件,查询完成后发生
33client.GeocodeCompleted+=newEventHandler(OnGeocodeCompleted);
34
35//创建一个Geocode检索请求
36GeocodeRequestrequest=newGeocodeRequest();
37//设置地图证书
38request.Credentials=newMicrosoft.Phone.Controls.Maps.Credentials();
39request.Credentials.ApplicationId=((ApplicationIdCredentialsProvider)map1.CredentialsProvider).ApplicationId;
40//设置检索条件
41request.Query=strAddress;
42//异步调用Geocode服务方法
43client.GeocodeAsync(request,waypointIndex);
44}
45catch(Exceptionex)
46{
47MessageBox.Show("地理信息编码服务解析过程中出现错误.");
48}
49}
50
51//使用全局变量来存储起点和终点的位置信息
52internalGeocodeResult[]geocodeResults;
53
54///
55 ///当检索地理信息完成后的回调函数
56 ///
57 ///
58 ///
59privatevoidOnGeocodeCompleted(objectsender,GeocodeCompletedEventArgse)
60{
61if(e.Error==null)
62{
63intwaypointIndex=System.Convert.ToInt32(e.UserState);
64geocodeResults[waypointIndex]=e.Result.Results[0];
65
66booldoneGeocoding=true;
67
68foreach(GeocodeService.GeocodeResultgringeocodeResults)
69{
70if(gr==null)
71{
72doneGeocoding=false;
73}
74}
75
76//当起点和终点位置查询都已经完成时,调用Route服务查询路线
77if(doneGeocoding)
78{
79CalculateRoute(geocodeResults);
80}
81}
82}
83
84///
85 ///根据起点终点坐标查询,使用路由服务查询路线
86 ///
87 ///
88privatevoidCalculateRoute(GeocodeResult[]results)
89{
90RouteServiceClientrouteService=newRouteServiceClient("BasicHttpBinding_IRouteService");
91//绑定事件回调函数
92routeService.CalculateRouteCompleted+=newEventHandler(routeService_CalculateRouteCompleted);
93
94//设置地图证书
95RouteRequestrouteRequest=newRouteRequest();
96routeRequest.Credentials=newCredentials();
97routeRequest.Credentials.ApplicationId=((ApplicationIdCredentialsProvider)map1.CredentialsProvider).ApplicationId;
98
99//实例化一个路由节点
100routeRequest.Options=newRouteOptions();
101routeRequest.Options.RoutePathType=RoutePathType.Points;
102
103//将保存Geocode服务查询结果的变量赋给到Route服务的waypoints属性中
104routeRequest.Waypoints=newObservableCollection();
105foreach(GeocodeResultresultinresults)
106{
107routeRequest.Waypoints.Add(GeocodeResultToWaypoint(result));
108}
109
110//向路由服务发送异步请求
111routeService.CalculateRouteAsync(routeRequest);
112
113}
114
115///
116 ///解析Geocode服务返回的数据
117 ///
118 ///
119 ///
120privateWaypointGeocodeResu