如何从 Windows Phone 的定位服务获取数据

 转自MSDN:http://msdn.microsoft.com/zh-cn/library/ff431782(v=vs.92).aspx

本主题介绍如何初始化定位服务、处理服务状态变化以及获取位置数据。若要了解和下载使用定位服务的示例应用程序,请参阅 Windows Phone 的代码示例

若要使用定位服务托管 API,您必须首先向您的应用程序中添加对 System.Device.dll 的引用。

添加对定位服务 DLL 的引用的步骤

  1. 在 Visual Studio 中打开一个新的或现有的 Windows Phone 解决方案。

  2. “项目”菜单中,选择“添加引用...”

  3. 在 .NET 选项卡上,选择组件名称“System.Device”,然后单击“确定”

接下来,向将使用定位服务的页面的 .xaml.cs 文件顶部添加一个 using 指令。

using System.Device.Location; 

为将用于访问定位服务的 GeoCoordinateWatcher 对象添加一个变量声明。给出变量类范围,以便只要显示该页面,它就会保留在内存中。

public partial class MainPage : PhoneApplicationPage {   GeoCoordinateWatcher watcher; 

下面的代码演示如何从定位服务获取数据。

从定位服务获取数据

  1. 使用 GeoCoordinateWatcher 对象的 Start 方法开始从定位服务获取数据。只要一加载该页面,就可以调用此方法。但是,您可以通过允许用户选择何时启动该服务来允许用户对耗电量有更多的控制权限。例如,您可以在某个按钮单击事件的处理程序中启动该服务。

    // Click the event handler for the “Start Location” button. private void startLocationButton_Click(object sender, RoutedEventArgs e) {   // The watcher variable was previously declared as type GeoCoordinateWatcher.    if (watcher == null)   {     watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High); // using high accuracy     watcher.MovementThreshold = 20; // use MovementThreshold to ignore noise in the signal 
  2. 为 StatusChanged 和 PositionChanged 事件添加事件处理程序。

        watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);     watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);   } 
  3. 现在,启动定位服务。此版本的 Start()()()() 方法开始从该服务异步获取数据,因此当应用程序服务尝试启动和获取数据时,用户可以继续使用您的应用程序。

      watcher.Start(); } // End of the Start button Click handler. 
  4. 实现 StatusChanged 事件处理程序。当定位服务的状态发生更改时引发此事件。在 GeoPositionStatusChangedEventArgs 对象中传递的 GeoPositionStatus 枚举告知您该服务的当前状态。可以使用它在应用程序中启用基于位置的功能,以及将服务的当前状态通知给用户。如果服务的状态为 Disabled,则可以检查Permission 属性,看用户是否禁用了应用程序的定位服务功能。

    // Event handler for the GeoCoordinateWatcher.StatusChanged event. void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e) {     switch (e.Status)     {         case GeoPositionStatus.Disabled:             // The Location Service is disabled or unsupported.             // Check to see whether the user has disabled the Location Service.             if (watcher.Permission == GeoPositionPermission.Denied)             {                 // The user has disabled the Location Service on their device.                 statusTextBlock.Text = "you have this application access to location.";             }             else             {                 statusTextBlock.Text = "location is not functioning on this device";             }             break;          case GeoPositionStatus.Initializing:             // The Location Service is initializing.             // Disable the Start Location button.             startLocationButton.IsEnabled = false;             break;          case GeoPositionStatus.NoData:             // The Location Service is working, but it cannot get location data.             // Alert the user and enable the Stop Location button.             statusTextBlock.Text = "location data is not available.";             stopLocationButton.IsEnabled = true;             break;          case GeoPositionStatus.Ready:             // The Location Service is working and is receiving location data.             // Show the current position and enable the Stop Location button.             statusTextBlock.Text = "location data is available.";             stopLocationButton.IsEnabled = true;             break;     } }  
  5. 当定位服务已准备就绪并接收数据时,它将开始引发 PositionChanged 事件,并且调用您应用程序的处理程序(如果您已实现)。在事件处理程序中,访问GeoPositionChangedEventArgs<(Of <(<'T>)>)> 对象的 Position 成员。Position 字段是一个 GeoPosition 对象,它由一个 Timestamp 和一个包含用于阅读的位置信息的 GeoCoordinate 对象组成。该示例访问纬度和经度值。

    void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e) {   latitudeTextBlock.Text = e.Position.Location.Latitude.ToString("0.000");   longitudeTextBlock.Text = e.Position.Location.Longitude.ToString("0.000"); } 
  6. 请确保在不需要时停止定位服务,以便最大程度提高设备的电池使用时间。对于该示例,实现一个“停止定位”按钮以允许用户停止定位服务。

    // Click the event handler for the “Start Location” button. private void stopLocationButton_Click(object sender, RoutedEventArgs e) {   watcher.Stop(); }  

 

posted @ 2012-08-02 16:17  therockthe  阅读(228)  评论(0编辑  收藏  举报