Windows Phone 8获取本机位置

来源:互联网 发布:淘宝宝贝分类怎么设置 编辑:程序博客网 时间:2024/03/29 19:11
private Geolocator mMyGeolocator = null;// 这里给出完整的命名空间private System.Device.Location.Geocoordinate mMyGeoCoordinate = null;/// <summary>        /// 定位到本机位置        /// </summary>       private async void getMyLocation()       {           try           {                         // Get my current location.               if (mMyGeolocator == null)               {                   mMyGeolocator = new Geolocator();                   mMyGeolocator.DesiredAccuracy = PositionAccuracy.High;                   mMyGeolocator.MovementThreshold = 30;                   mMyGeolocator.DesiredAccuracyInMeters = 50;   // 状态改变事件的委托函数                   mMyGeolocator.StatusChanged += myGeolocator_StatusChanged;   // 位置改变的委托函数                   mMyGeolocator.PositionChanged += myGeolocator_PositionChanged;               }   // 获得原始数据,类型为Geoposition               Geoposition myGeoposition = await mMyGeolocator.GetGeopositionAsync(maximumAge: TimeSpan.FromMinutes(2),                                                                                        timeout: TimeSpan.FromSeconds(10));               // 获得我的坐标的原始数据,注意这里得到的数据与mMyGeoCoordinate类型不同,所有需要转换               Windows.Devices.GeolocationGeocoordinate myGeocoordinateTemp = myGeoposition.Coordinate;               // 转换后的真实经纬度               mMyGeoCoordinate = CoordinateConverter.ConvertGeocoordinate(myGeocoordinateTemp);           }           catch (Exception e)           {               DebugMsg.debug("getMyLocation 错误 : " + e.ToString());           }       }       /// <summary>       /// 状态改变       /// </summary>       /// <param name="sender"></param>       /// <param name="e"></param>       void myGeolocator_StatusChanged(Geolocator sender, StatusChangedEventArgs args)       {           switch (args.Status)           {               case PositionStatus.Disabled:                   MessageBox.Show("位置服务没有开启,请到[ 设置-位置 ]页面开启位置服务");                   break;               case PositionStatus.Initializing:                   DebugMsg.debug("位置服务初始化中...");                   break;               case PositionStatus.NoData:                   DebugMsg.debug("没有获取到位置数据,请重试...");                   break;               case PositionStatus.Ready:                   DebugMsg.debug("正在获取GPS数据...");                                   break;           }       }       /// <summary>       /// 位置改变后定位到该点,在更新我的位置。这样就会实现实时的位置显示       /// </summary>       /// <param name="sender"></param>       /// <param name="e"></param>       void myGeolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs e)       {           // 获取到位置数据           Geocoordinate coordinateTemp = e.Position.Coordinate;           // mMyGeoCoordinate与coordinateTemp的类型不同,需要转换一下           mMyGeoCoordinate = CoordinateConverter.ConvertGeocoordinate(coordinateTemp);       }


转换类的代码:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Device.Location;       // Provides the GeoCoordinate class.using Windows.Devices.Geolocation;  //Provides the Geocoordinate class.namespace CommuteGreener.Utils{    public static class CoordinateConverter    {        public static GeoCoordinate ConvertGeocoordinate(Geocoordinate geocoordinate)        {            return new GeoCoordinate                (                geocoordinate.Latitude,                geocoordinate.Longitude,                geocoordinate.Altitude ?? Double.NaN,                geocoordinate.Accuracy,                geocoordinate.AltitudeAccuracy ?? Double.NaN,                geocoordinate.Speed ?? Double.NaN,                geocoordinate.Heading ?? Double.NaN                );        }    }}


原创粉丝点击