Windows Phone 8获取本机位置

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值