Windows Phone开发教程6文档格式.docx
- 文档编号:16608947
- 上传时间:2022-11-24
- 格式:DOCX
- 页数:15
- 大小:403.48KB
Windows Phone开发教程6文档格式.docx
《Windows Phone开发教程6文档格式.docx》由会员分享,可在线阅读,更多相关《Windows Phone开发教程6文档格式.docx(15页珍藏版)》请在冰豆网上搜索。
Map>
6<
7<
Grid>
8<
ButtonContent="
航测模式"
HorizontalAlignment="
Left"
Width="
160"
Name="
btnAerial"
Click="
btnAerial_Click"
<
/Button>
9<
道路模式"
Right"
btnRoad"
btnRoad_Click"
10<
/Grid>
11<
/StackPanel>
12<
13<
14<
放大"
btnZoomIn"
btnZoomIn_Click"
15<
缩小"
btnZoomOut"
btnZoomOut_Click"
16<
17<
18<
19<
复制代码
这里给出后台程序MainPage.xaml.cs的处理代码:
1 ///<
summary>
2 ///放大
3 ///<
/summary>
4 ///<
paramname="
sender"
/param>
5 ///<
e"
6privatevoidbtnZoomIn_Click(objectsender,RoutedEventArgse)
7{
8doublezoom;
9zoom=map1.ZoomLevel;
10map1.ZoomLevel=++zoom;
11}
12
13///<
14 ///缩小
15 ///<
16 ///<
17 ///<
18privatevoidbtnZoomOut_Click(objectsender,RoutedEventArgse)
19{
20doublezoom;
21zoom=map1.ZoomLevel;
22map1.ZoomLevel=--zoom;
23}
24
25///<
26 ///道路模式
27 ///<
28 ///<
29 ///<
30privatevoidbtnRoad_Click(objectsender,RoutedEventArgse)
31{
32map1.Mode=newRoadMode();
33}
34
35///<
36 ///航测模式
37 ///<
38 ///<
39 ///<
40privatevoidbtnAerial_Click(objectsender,RoutedEventArgse)
41{
42map1.Mode=newAerialMode(true);
//参数为true的构造函数表示显示地理信息
43}
可以看到,Map控件已经把很多常用的功能封装得非常好了,只需要我们简单的调用就可以了,下面是程序运行后的效果:
3.在Map中使用位置服务(定位)和图钉(pushpin)
上一篇笔记讲到了WindowsPhone的位置服务,现在我们来学习如何在Map中使用位置服务,在地图中对我们的所在地进行定位;
以及如何在地图中添加图钉(Pushpin)
增强地图应用的可用性。
下面让我们来观察一个示例程序:
MainPage.xaml:
1 <
2<
3<
500"
VerticalAlignment="
Top"
你注册的MapKey"
/>
4<
定位"
Bottom"
btn_Location"
btn_Location_Click"
5<
后台处理程序,MainPage.xaml.cs:
1publicpartialclassMainPage:
PhoneApplicationPage
2{
3GeoCoordinateWatcherwatcher;
4//构造函数
5publicMainPage()
6{
7InitializeComponent();
8}
9
10///<
11 ///在地图上定位
12 ///<
13 ///<
14 ///<
15privatevoidbtn_Location_Click(objectsender,RoutedEventArgse)
16{
17if(watcher==null)
18{
19watcher=newGeoCoordinateWatcher(GeoPositionAccuracy.High);
//采用高精度
20watcher.MovementThreshold=20;
//PositionChanged事件之间传送的最小距离
21 //绑定事件
22watcher.PositionChanged+=newEventHandler<
GeoPositionChangedEventArgs<
GeoCoordinate>
(watcher_PositionChanged);
23//开始从位置服务获取数据
24watcher.Start();
25}
26}
27
28//检测到位置更改时
29 //当定位服务已准备就绪并接收数据时,它将开始引发PositionChanged事件
30voidwatcher_PositionChanged(objectsender,GeoPositionChangedEventArgs<
e)
32Locationlocation=newLocation();
//实例化一个位置位置类的实例
33 //将当前的经纬度值赋给位置对象
34location.Latitude=e.Position.Location.Latitude;
35location.Longitude=e.Position.Location.Longitude;
36
37Pushpinpushpin=newPushpin();
//实例化一个图钉实例
38pushpin.Content="
当前位置"
;
39pushpin.Location=location;
40
41this.map1.Children.Add(pushpin);
//将图钉显示在地图上
42this.map1.SetView(location,10);
//缩放界别为10(1-16),始终将当前位置位于地图中心
44}
当我们点击"
按钮后,通过使用模拟器的位置模拟工具来模拟当前的位置,可以发现手机上的地图会始终将当前位置位于地图中心,并且会加上一个图钉来表示当前位置,下面的程序运行的效果:
4.使用BingMapsSOAPServices
BingMaps提供了一组SOAPServices,通过使用这些服务可以使我们能够使我们的应用实现地理位置计算、路径计算等额外的功能。
现在让我们一起来了解如何使用其中的两个服务。
∙地理编码服务(GeocodeService),服务地址是:
∙路由功能服务(RouteService),服务的地址是:
地理编码服务可以通过搜索地名来匹配在地图上对应的位置(经度和纬度),或者通过实际位置的经度和纬度来确定对应在地图上的位置。
路由功能服务可以通过计算地点的坐标来显示线路的功能,比如在XX地图中常用的驾车线路功能等。
下面让我们通过一个示例来了解如何使用这些服务,在WindowsPhone中实现我们在地图应用中常见的出行路径计算的功能。
首先添加前面给出地址的两个服务引用,下面让代码来说话:
MainPage.xaml代码:
1 <
--LayoutRoot是包含所有页面内容的根网格-->
LayoutRoot"
Background="
Transparent"
Grid.RowDefinitions>
RowDefinitionHeight="
auto"
*"
/Grid.RowDefinitions>
0"
GridHeight="
530"
MapKey"
/>
GridGrid.Row="
>
StackPanelOrientation="
Vertical"
TextBlockText="
起点:
"
Center"
FontSize="
28"
/TextBlock>
TextBoxWidth="
400"
80"
txtStartPath"
/TextBox>
20<
终点:
21<
txtStopPath"
22<
23<
ButtonHeight="
Content="
查找路径"
btnQueryPath"
btnQueryPath_Click"
24<
25<
26<
后台处理程序,代码比较长:
1publicpartialclassMainPage:
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 ///<
strGeocodeInfo"
26publicvoidGetGeocodeAsync(stringstrAddress,intwaypointIndex)
27{
28try
29{
30//实例化GeocodeService客户端对象
31GeocodeServiceClientclient=newGeocodeServiceClient("
BasicHttpBinding_IGeocodeService"
);
32//绑定事件,查询完成后发生
33client.GeocodeCompleted+=newEventHandler<
GeocodeCompletedEventArgs>
(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 ///<
results"
88privatevoidCalculateRoute(GeocodeResult[]results)
89{
90RouteServiceClientrouteService=newRouteServiceClient("
BasicHttpBinding_IRouteService"
91//绑定事件回调函数
92routeService.CalculateRouteCompleted+=newEventHandler<
RouteService.CalculateRouteCompletedEventArgs>
(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<
RouteService.Waypoint>
();
105foreach(GeocodeResultresultinresults)
106{
107routeRequest.Waypoints.Add(GeocodeResultToWaypoint(result));
108}
109
110//向路由服务发送异步请求
111routeService.CalculateRouteAsync(routeRequest);
112
113}
114
115///<
116 ///解析Geocode服务返回的数据
117 ///<
118 ///<
result"
119 ///<
returns>
/returns>
120privateWaypointGeocodeResu
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Windows Phone开发教程6 Phone 开发 教程