Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

在Windows Phone地图控件上绘制当前位置

From Wiki
Jump to: navigation, search
SignpostIcon HereMaps 99.png
SignpostIcon WP7 70px.png
Article Metadata

代码示例
兼容于
文章
翻译:
WS - OtomiiLu
最后由 hamishwillee 在 26 Jun 2013 编辑

本文演示如何根据GeoCoordinateWatcher类的位置信息在Map上缩放到用户位置

Contents

介绍

这个示例应用程序创建了一个地图.当用户点击my location 菜单, 该应用程序获取当前移动设备的位置并在地图的缩放视图显示经度和纬度. 当前位置是通过GeoCoordinateWatcher类提取的(从 System.Device.Location 命名空间).

MapControls.png

当前位置

实现

首先用Windows Phone Application 模板创建一个项目. 创建地图(MainPage.xaml) 项目创建好后, 从Visual Studio 2010工具箱中拖拽一个新地图控件到页面上. 其将在XAMLW文件生成地图控件代码.

<my:Map Height="408" HorizontalAlignment="Left" Margin="6,20,0,0" Name="map1" VerticalAlignment="Top" Width="444" Mode="AerialWithLabels" CredentialsProvider="{StaticResource BingMapsAPIKey}" >
</my:Map>

使用地图,你需要有Bing地图密钥, 它可以从http://www.bingmapsportal.com/ 获得. 你可以用你的密钥(如下显示)替换{StaticResource BingMapsAPIKey}:

<my:Map Height="408" HorizontalAlignment="Left" Margin="6,20,0,0" Name="map1" VerticalAlignment="Top" Width="444" Mode="AerialWithLabels" CredentialsProvider="Anqep9VR0k6VxcmRp4qJ_AYWglilkC3B4c2QgZAgZTBS-bCr2GOI7iOopDGd-PXa" >
</my:Map>

另外你可以在你的App.xaml文件中声明BingMapsAPIKey 静态资源- 注意:如果你计划该应用程序任意地方使用该密钥,这是推荐的.

<Application.Resources>
<my:ApplicationIdCredentialsProvider ApplicationId="Anqep9VR0k6VxcmRp4qJ_AYWglilkC3B4c2QgZAgZTBS-bCr2GOI7iOopDGd-PXa" x:Key="BingMapsAPIKey"></my:ApplicationIdCredentialsProvider>
</Application.Resources>

得到坐标并缩放

现在我们添加My Location菜单当用户点击菜单就调用编有代码获取位置的ApplicationBarMenuItem_Click().

private void ApplicationBarMenuItem_Click(object sender, EventArgs e)
{
GeoCoordinateWatcher gcw;
gcw = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
gcw.MovementThreshold = 20;
gcw.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(gcw_StatusChanged);
gcw.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(gcw_PositionChanged);
gcw.Start();
map1.Center = new GeoCoordinate(gcw.Position.Location.Latitude, gcw.Position.Location.Longitude);
if (gcw != null) gcw.Stop();
map1.ZoomLevel = 17;
map1.ZoomBarVisibility = Visibility.Visible;
map1.ScaleVisibility = Visibility.Visible;
}

我们创建一个GeoCoordinateWatcher 实例并设置精确度高阈值为20,其接近20米左右. GeoPositionStatusChangedEventArgs 事件取得已更新的状态,GeoPositionChangedEventArgs 事件取得对应改事件的位置数据,如经度和纬度. Start()方法启用PositionChanged事件并允许访问该位置属性. 当我们得到数据就把值放进地图并在地图中心位置显示该位置. 最后使用Stop()停止GeoCoordinateWatcher提供位置数据和事件. 我们也要设置地图的缩放水平属性.

测试

本应用程序仅在模拟器上测试了. 以下文章讨论如何在模拟器上测试地图:

Windows Phone GPS 模拟器

这里

源代码

该示例完整源代码这里可用: File:MapControls.zip

This page was last modified on 26 June 2013, at 10:58.
143 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×