与众不同 windows phone (44) - 8.0 位置和地图
与众不同 windows phone (44) - 8.0 位置和地图
作者:webabcd
介绍
与众不同 windows phone 8.0 之 位置和地图
- 位置(GPS) - Location API
- 诺基亚地图
示例
1、演示新 Location API 的应用
GPS/Demo.xaml
<phone:PhoneApplicationPage x:Class="Demo.GPS.Demo" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" FontFamily="{StaticResource PhoneFontFamilyNormal}" FontSize="{StaticResource PhoneFontSizeNormal}" Foreground="{StaticResource PhoneForegroundBrush}" SupportedOrientations="Portrait" Orientation="Portrait" mc:Ignorable="d" shell:SystemTray.IsVisible="True"> <Grid x:Name="LayoutRoot"> <StackPanel> <TextBlock Name="lblMsg" TextWrapping="Wrap" /> <Button x:Name="btnDemo" Content="通过 GPS 获取当前位置" Click="btnDemo_Click" /> </StackPanel> </Grid> </phone:PhoneApplicationPage>
GPS/Demo.xaml.cs
/* * 演示新 Location API 的应用 * * wp7 时代的 Location API 也是支持的(能不用就别用了),参见:http://www.cnblogs.com/webabcd/archive/2012/08/09/2629636.html * * * 注: * 1、需要在 manifest 中增加配置 <Capability Name="ID_CAP_LOCATION" /> * 2、在获取位置数据之前,需要提供隐私策略并得到用户的允许 * 3、目前 wp 机器的位置提供程序都是 GPS */ using System; using System.Windows; using Microsoft.Phone.Controls; using Windows.Devices.Geolocation; namespace Demo.GPS { public partial class Demo : PhoneApplicationPage { // 新的 Location API Geolocator geolocator; public Demo() { InitializeComponent(); } protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) { geolocator = new Geolocator(); // 期望的精度级别(PositionAccuracy.Default 或 PositionAccuracy.High) geolocator.DesiredAccuracy = PositionAccuracy.High; // 期望的数据精度(米) geolocator.DesiredAccuracyInMeters = 50; // 移动距离超过此值后,触发 PositionChanged 事件 geolocator.MovementThreshold = 100; // 在两次位置更新的时间点中间,请求位置数据的最小间隔(毫秒) geolocator.ReportInterval = 0; // 位置更新时触发的事件 geolocator.PositionChanged += geolocator_PositionChanged; // 位置服务的状态发生改变时触发的事件 geolocator.StatusChanged += geolocator_StatusChanged; base.OnNavigatedTo(e); } protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e) { geolocator.PositionChanged -= geolocator_PositionChanged; geolocator.StatusChanged -= geolocator_StatusChanged; geolocator = null; base.OnNavigatedFrom(e); } private async void btnDemo_Click(object sender, RoutedEventArgs e) { try { // 获取位置信息 Geoposition geoposition = await geolocator.GetGeopositionAsync(); lblMsg.Text = "位置精度(米): " + geoposition.Coordinate.Accuracy.ToString(); lblMsg.Text += Environment.NewLine; lblMsg.Text += "海拔精度(米): " + geoposition.Coordinate.AltitudeAccuracy.ToString(); lblMsg.Text += Environment.NewLine; lblMsg.Text += "纬度: " + geoposition.Coordinate.Latitude.ToString("0.00"); lblMsg.Text += Environment.NewLine; lblMsg.Text += "经度: " + geoposition.Coordinate.Longitude.ToString("0.00"); lblMsg.Text += Environment.NewLine; lblMsg.Text += "海拔(米): " + geoposition.Coordinate.Altitude.ToString(); lblMsg.Text += Environment.NewLine; lblMsg.Text += "行进方向(相对于正北的度数): " + geoposition.Coordinate.Heading.ToString(); lblMsg.Text += Environment.NewLine; lblMsg.Text += "行进速度(米/秒): " + geoposition.Coordinate.Speed.ToString(); lblMsg.Text += Environment.NewLine; lblMsg.Text += "确定位置的时间(UTC0): " + geoposition.Coordinate.Timestamp.ToString("yyyy-MM-dd hh:mm:ss"); lblMsg.Text += Environment.NewLine; lblMsg.Text += "数据源(Satellite, WiFi, Cellular): " + geoposition.Coordinate.PositionSource.ToString(); lblMsg.Text += Environment.NewLine; lblMsg.Text += "卫星的位置精度衰减: " + geoposition.Coordinate.SatelliteData.PositionDilutionOfPrecision.ToString(); lblMsg.Text += Environment.NewLine; lblMsg.Text += "卫星的水平精度衰减: " + geoposition.Coordinate.SatelliteData.HorizontalDilutionOfPrecision.ToString(); lblMsg.Text += Environment.NewLine; lblMsg.Text += "卫星的垂直精度衰减: " + geoposition.Coordinate.SatelliteData.VerticalDilutionOfPrecision.ToString(); lblMsg.Text += Environment.NewLine; lblMsg.Text += Environment.NewLine; if (geoposition.CivicAddress != null) { lblMsg.Text = "国家名称: " + geoposition.CivicAddress.Country; lblMsg.Text += Environment.NewLine; lblMsg.Text = "省名称: " + geoposition.CivicAddress.State; lblMsg.Text += Environment.NewLine; lblMsg.Text = "城市名称: " + geoposition.CivicAddress.City; lblMsg.Text += Environment.NewLine; lblMsg.Text = "邮编: " + geoposition.CivicAddress.PostalCode; lblMsg.Text += Environment.NewLine; lblMsg.Text = "确定位置的时间(UTC0): " + geoposition.CivicAddress.Timestamp; } } catch (Exception ex) { if ((uint)ex.HResult == 0x80004004) { lblMsg.Text = "定位服务当前是关闭状态,请打开它"; } else { lblMsg.Text = ex.ToString(); } } } // 位置服务的状态变化了 void geolocator_StatusChanged(Geolocator sender, StatusChangedEventArgs args) { // 获取位置服务的状态 PositionStatus status = geolocator.LocationStatus; // 获取位置服务的状态 status = args.Status; switch (args.Status) { case PositionStatus.Disabled: // 位置提供程序已禁用,即用户尚未授予应用程序访问位置的权限 break; case PositionStatus.Initializing: // 初始化中 break; case PositionStatus.NoData: // 无有效数据 break; case PositionStatus.Ready: // 已经准备好了相关数据 break; case PositionStatus.NotAvailable: // 位置服务传感器不可用 break; case PositionStatus.NotInitialized: // 尚未初始化 break; } } // 位置变化了 void geolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args) { this.Dispatcher.BeginInvoke(delegate() { Geoposition geoposition = args.Position; lblMsg.Text = "纬度: " + geoposition.Coordinate.Latitude.ToString("0.00"); lblMsg.Text += Environment.NewLine; lblMsg.Text += "经度: " + geoposition.Coordinate.Longitude.ToString("0.00"); }); } } }
2、演示诺基亚地图的应用
Map/Demo.xaml
<phone:PhoneApplicationPage x:Class="Demo.Map.Demo" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" FontFamily="{StaticResource PhoneFontFamilyNormal}" FontSize="{StaticResource PhoneFontSizeNormal}" Foreground="{StaticResource PhoneForegroundBrush}" SupportedOrientations="Portrait" Orientation="Portrait" mc:Ignorable="d" shell:SystemTray.IsVisible="True" xmlns:maps="clr-namespace:Microsoft.Phone.Maps.Controls;assembly=Microsoft.Phone.Maps"> <Grid x:Name="LayoutRoot" Background="Transparent"> <StackPanel> <!-- Microsoft.Phone.Maps.Controls.Map - 诺基亚地图 CenterChanged - 地图的中心点发生变化时触发的事件 --> <maps:Map x:Name="map" Width="480" Height="440" CenterChanged="map_CenterChanged" /> <StackPanel Orientation="Horizontal"> <Button Name="btnRoad" Content="道路图" Click="btnRoad_Click" /> <Button Name="btnAerial" Content="卫星图" Click="btnAerial_Click" /> </StackPanel> <StackPanel Orientation="Horizontal"> <Button Name="btnHybrid" Content="卫星图叠加道路图" Click="btnHybrid_Click" /> <Button Name="btnTerrain" Content="自然地形叠加道路图" Click="btnTerrain_Click" /> </StackPanel> <StackPanel Orientation="Horizontal"> <Button Name="btnZoomIn" Content="放大" Click="btnZoomIn_Click" /> <Button Name="btnZoomOut" Content="缩小" Click="btnZoomOut_Click" /> </StackPanel> <StackPanel Orientation="Horizontal"> <Button Name="btnMoveTop" Content="上移" Click="btnMoveTop_Click" /> <Button Name="btnMoveBottom" Content="下移" Click="btnMoveBottom_Click" /> <Button Name="btnMoveLeft" Content="左移" Click="btnMoveLeft_Click" /> <Button Name="btnMoveRight" Content="右移" Click="btnMoveRight_Click" /> </StackPanel> <TextBlock Name="lblMsg" /> </StackPanel> </Grid> </phone:PhoneApplicationPage>
Map/Demo.xaml.cs
/* * 演示诺基亚地图的应用 * * * 本例仅用于说明诺基亚地图的基本使用方式,更多的内容,如:路线绘制,图钉等功能请参见 Microsoft.Phone.Maps.Controls.Map 控件文档和 Windows Phone Toolkit 文档 * * * 注: * 1、需要在 manifest 中增加配置 <Capability Name="ID_CAP_MAP" /> * 2、相关的 Launcher 参见本项目的 Launchers 文件夹内的地图相关的演示 * 3、Bing 地图虽然仍可用,但是能不用就别用了 * 4、关于 pin 之类的地图扩展,请使用 Windows Phone Toolkit,参见 http://phone.codeplex.com/ * * * 另:与地图相关的协议说明如下,你的 app 如果支持这些协议将会在用户请求时启动(只有你一个 app 支持时)或出现在启动列表中(多个 app 支持时) * 1、驾车到指定地点:ms-drive-to:?destination.latitude=<latitude>&destination.longitude=<longitude>&destination.name=<name> * 2、散步到指定地点:ms-walk-to:?destination.latitude=<latitude>&destination.longitude<longitude>&destination.name=<name> */ using System.Windows; using Microsoft.Phone.Controls; using System.Device.Location; using Microsoft.Phone.Maps.Controls; namespace Demo.Map { public partial class Demo : PhoneApplicationPage { // 地图的中心点,经纬度坐标 private GeoCoordinate _center; // ZoomLevel: 1 - 最小, 20 - 最大 private double _zoomLevel = 16; // Heading: 0 到 360 之间的任意数字。例:上为正北则为0,上为正西则为90,上为正南则为180,上为正东则为270 private double _heading = 0; // Pitch: 0 到 180 之间的任意数字,代表地图绕 X 轴倾斜的角度(以实现 3D 效果) private double _pitch = 15; public Demo() { InitializeComponent(); this.Loaded += Demo_Loaded; } void Demo_Loaded(object sender, RoutedEventArgs e) { map.LandmarksEnabled = true; // 是否显示地标(ZoomLevel 16 级以上才会显示) map.PedestrianFeaturesEnabled = true; // 是否显示步行街(ZoomLevel 16 级以上才会显示) map.ColorMode = MapColorMode.Light; // 颜色模式(Light 或 Dark) _center = new GeoCoordinate(39.909, 116.397); // map.Center = _center; // map.ZoomLevel = _zoomLevel; // map.Heading = _heading; // map.Pitch = _pitch; // SetView() - 通过指定的 Center, ZoomLevel, Heading, Pitch, MapAnimationKind 参数来显示地图 // MapAnimationKind - 代表地图过渡时的动画效果。None:无动画;Linear:线性动画;Parabolic:抛物线动画 map.SetView(_center, _zoomLevel, _heading, _pitch, MapAnimationKind.Parabolic); } private void btnRoad_Click(object sender, RoutedEventArgs e) { // 道路地图 map.CartographicMode = MapCartographicMode.Road; } private void btnAerial_Click(object sender, RoutedEventArgs e) { // 卫星地图 map.CartographicMode = MapCartographicMode.Aerial; } private void btnHybrid_Click(object sender, RoutedEventArgs e) { // 卫星地图上叠加道路地图 map.CartographicMode = MapCartographicMode.Hybrid; } private void btnTerrain_Click(object sender, RoutedEventArgs e) { // 自然地形地图上叠加道路地图 map.CartographicMode = MapCartographicMode.Terrain; } private void btnZoomIn_Click(object sender, RoutedEventArgs e) { // 放大地图 map.SetView(_center, ++_zoomLevel, _heading, _pitch, MapAnimationKind.Linear); } private void btnZoomOut_Click(object sender, RoutedEventArgs e) { // 缩小地图 map.SetView(_center, --_zoomLevel, _heading, _pitch, MapAnimationKind.Linear); } private void btnMoveTop_Click(object sender, RoutedEventArgs e) { // 上移地图,可以看到地图切换时线性过渡的效果(MapAnimationKind.Linear) _center = new GeoCoordinate(map.Center.Latitude + 0.1, map.Center.Longitude); map.SetView(_center, _zoomLevel, _heading, _pitch, MapAnimationKind.Linear); } private void btnMoveBottom_Click(object sender, RoutedEventArgs e) { // 下移地图,可以看到地图切换时线性过渡的效果(MapAnimationKind.Linear) _center = new GeoCoordinate(map.Center.Latitude -0.1, map.Center.Longitude); map.SetView(_center, _zoomLevel, _heading, _pitch, MapAnimationKind.Linear); } private void btnMoveLeft_Click(object sender, RoutedEventArgs e) { // 左移地图,可以看到地图切换时抛物线过渡的效果(MapAnimationKind.Parabolic) _center = new GeoCoordinate(map.Center.Latitude, map.Center.Longitude - 0.1); map.SetView(_center, _zoomLevel, _heading, _pitch, MapAnimationKind.Parabolic); } private void btnMoveRight_Click(object sender, RoutedEventArgs e) { // 右移地图,可以看到地图切换时抛物线过渡的效果(MapAnimationKind.Parabolic) _center = new GeoCoordinate(map.Center.Latitude, map.Center.Longitude + 0.1); map.SetView(_center, _zoomLevel, _heading, _pitch, MapAnimationKind.Parabolic); } private void map_CenterChanged(object sender, MapCenterChangedEventArgs e) { // CenterChanged - 中心点发生变化时触发的事件 // 类似的事件还有:CartographicModeChanged, HeadingChanged, PitchChanged, ZoomLevelChanged, ViewChanging, ViewChanged 等等 lblMsg.Text = string.Format("经度:{0},纬度{1}", map.Center.Longitude, map.Center.Latitude); } } }
OK
[源码下载]