[源码下载]

与众不同 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 = ; // 移动距离超过此值后,触发 PositionChanged 事件
geolocator.MovementThreshold = ;
// 在两次位置更新的时间点中间,请求位置数据的最小间隔(毫秒)
geolocator.ReportInterval = ; // 位置更新时触发的事件
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 = ;
// Heading: 0 到 360 之间的任意数字。例:上为正北则为0,上为正西则为90,上为正南则为180,上为正东则为270
private double _heading = ;
// Pitch: 0 到 180 之间的任意数字,代表地图绕 X 轴倾斜的角度(以实现 3D 效果)
private double _pitch = ; 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
[源码下载]

与众不同 windows phone (44) - 8.0 位置和地图的更多相关文章

  1. 与众不同 windows phone (35) - 8.0 新的启动器: ShareMediaTask, SaveAppointmentTask, MapsTask, MapsDirectionsTask, MapDownloaderTask

    [源码下载] 与众不同 windows phone (35) - 8.0 新的启动器: ShareMediaTask, SaveAppointmentTask, MapsTask, MapsDirec ...

  2. 与众不同 windows phone (41) - 8.0 相机和照片: 通过 AudioVideoCaptureDevice 捕获视频和音频

    [源码下载] 与众不同 windows phone (41) - 8.0 相机和照片: 通过 AudioVideoCaptureDevice 捕获视频和音频 作者:webabcd 介绍与众不同 win ...

  3. 与众不同 windows phone (42) - 8.0 相机和照片: 通过 PhotoCaptureDevice 捕获照片

    [源码下载] 与众不同 windows phone (42) - 8.0 相机和照片: 通过 PhotoCaptureDevice 捕获照片 作者:webabcd 介绍与众不同 windows pho ...

  4. 与众不同 windows phone (34) - 8.0 新的控件: LongListSelector

    [源码下载] 与众不同 windows phone (34) - 8.0 新的控件: LongListSelector 作者:webabcd 介绍与众不同 windows phone 8.0 之 新的 ...

  5. 与众不同 windows phone (36) - 8.0 新的瓷贴: FlipTile, CycleTile, IconicTile

    [源码下载] 与众不同 windows phone (36) - 8.0 新的瓷贴: FlipTile, CycleTile, IconicTile 作者:webabcd 介绍与众不同 windows ...

  6. 与众不同 windows phone (37) - 8.0 文件系统: StorageFolder, StorageFile, 通过 Uri 引用文件, 获取 SD 卡中的文件

    [源码下载] 与众不同 windows phone (37) - 8.0 文件系统: StorageFolder, StorageFile, 通过 Uri 引用文件, 获取 SD 卡中的文件 作者:w ...

  7. 与众不同 windows phone (38) - 8.0 关联启动: 使用外部程序打开一个文件或URI, 关联指定的文件类型或协议

    [源码下载] 与众不同 windows phone (38) - 8.0 关联启动: 使用外部程序打开一个文件或URI, 关联指定的文件类型或协议 作者:webabcd 介绍与众不同 windows ...

  8. 与众不同 windows phone (39) - 8.0 联系人和日历

    [源码下载] 与众不同 windows phone (39) - 8.0 联系人和日历 作者:webabcd 介绍与众不同 windows phone 8.0 之 联系人和日历 自定义联系人存储的增删 ...

  9. 与众不同 windows phone (40) - 8.0 媒体: 音乐中心的新增功能, 图片中心的新增功能, 后台音乐播放的新增功能

    [源码下载] 与众不同 windows phone (40) - 8.0 媒体: 音乐中心的新增功能, 图片中心的新增功能, 后台音乐播放的新增功能 作者:webabcd 介绍与众不同 windows ...

随机推荐

  1. Mac下MySQL卸载方法 转载

    mac下mysql的DMG格式安装内有安装文件,却没有卸载文件……很郁闷的事. 网上搜了一下,发现给的方法原来得手动去删. 很多文章记述要删的文件不完整,后来在stackoverflow这里发现了一个 ...

  2. 移植UE4的模型操作到Unity中

    最近在Unity上要写一个东东,功能差不多就是在Unity编辑器上的旋转,移动这些,在手机上也能比较容易操作最好,原来用Axiom3D写过一个类似的,有许多位置并不好用,刚好在研究UE4的源码,在模型 ...

  3. (笔记)Linux内核学习(九)之内核内存管理方式

    一 页 内核把物理页作为内存管理的基本单位:内存管理单元(MMU)把虚拟地址转换为物理 地址,通常以页为单位进行处理.MMU以页大小为单位来管理系统中的也表. 32位系统:页大小4KB 64位系统:页 ...

  4. Golang pprof heap profile is empty

    Q: When you use `go tool pprof` get heap data, profile is empty. A: The default sampling rate is 1 s ...

  5. ASP.NET MVC 获取当前访问域名

    var request = filterContext.HttpContext.Request; string url = request.Url.Authority; string function ...

  6. keil l251 command summary --Lib

    keil l251 command summaryLIB251 LIST MYLIB.LIB TO MYLIB.LST PUBLICS LIB251 EXTRACT MYLIB.LIB (GOODCO ...

  7. SQL查询集合合并成字符串

    有时候需要查询某一个字段,并把查询结果组成一个字符串,则: ) SELECT @str=isnull(@str+',','')+列名 FROM 表名 SELECT @str

  8. Android UI开发第四十一篇——墨迹天气3.0引导界面及动画实现

    周末升级了墨迹天气,看着引导界面做的不错,模仿一下,可能与原作者的代码实现不一样,但是实现的效果还是差不多的.先分享一篇以前的文章,android动画的基础知识,<Android UI开发第十二 ...

  9. 转:Transform Web.Config when Deploying a Web Application Project

    Introduction One of the really cool features that are integrated with Visual Studio 2010 is Web.Conf ...

  10. SpringMVC学习系列-后记 结合SpringMVC和Hibernate-validator,根据后台验证规则自动生成前台的js验证代码

    在SpringMVC学习系列(6) 之 数据验证中我们已经学习了如何结合Hibernate-validator进行后台的数据合法性验证,但是通常来说后台验证只是第二道保险,为了更好的用户体验会现在前端 ...