Windows Phone获取经纬度信息

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

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

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

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

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

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

注意注意

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

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

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);
            }
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值