天天看点

window phone 获取经纬度信息

PS : 因为需要冷启动至少也需要一分钟以上,所以最好在公共类初始化,在需要调用的时候直接使用。

GeoCoordinateWatcher 类提供基于坐标的位置数据,其来自当前的位置提供程序。

来自当前位置提供程序的基于坐标的位置数据,该提供程序是计算机上优先级别最高的位置提供程序,其优先级别取决于一系列因素,如:来自所有提供程序的数据的存在时间和准确性、位置应用程序请求的准确性、与位置提供程序关联的电量消耗和性能影响。 当前位置提供程序可能会随时间改变,例如,当 GPS 设备失去内部附属信号功能并且 Wi-Fi 三边转换法提供程序成为计算机上最准确的提供程序时。

若要开始访问位置数据,请创建 GeoCoordinateWatcher,然后调用 Start 或 TryStart,开始从当前的位置提供程序获取数据。

可检查 Status 属性来确定数据是否可用。

如果数据可用,您可以从 Position 属性一次获取位置,或通过处理 PositionChanged 事件接收连续的位置更新。

Permission、Status 以及 Position 属性支持 INotifyPropertyChanged,因此应用程序可以数据绑定到这些属性。

在 Windows 7 中,如果位置提供程序已经安装并能够解析计算机的位置,则所有 System.Device.Location 类都完全正常。

注意
在 Windows 7 Starter 版上,唯一受支持的位置提供商是控制面板中的默认位置提供程序,且必须安装外接程序以指定纬度和经度。

注意:在

Windows 7 之前的 Windows 版本中,以下条件适用:

  • 可创建具有构造函数的所有 System.Device.Location 对象,但 Status 属性将始终具有值 Disabled。
  • Position 的 Location 属性所指示的位置将始终为 Unknown。
  • 不会引发位置事件。

using System;

using System.Device.Location;

namespace GetLocationEvent

{

class Program

{

static void Main(string[] args)

{

CLocation myLocation = new CLocation();

myLocation.GetLocationEvent();

Console.WriteLine("Enter any key to quit.");

Console.ReadLine();

}

class CLocation

{

GeoCoordinateWatcher watcher;

public void GetLocationEvent()

{

this.watcher = new GeoCoordinateWatcher();

this.watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);

bool started = this.watcher.TryStart(false, TimeSpan.FromMilliseconds(2000));

if (!started)

{

Console.WriteLine("GeoCoordinateWatcher timed out on start.");

}

}

void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)

{

PrintPosition(e.Position.Location.Latitude, e.Position.Location.Longitude);

}

void PrintPosition(double Latitude, double Longitude)

{

Console.WriteLine("Latitude: {0}, Longitude {1}", Latitude, Longitude);

}

}

}

}

window phone 获取经纬度信息