- 先在Package.appxmanifest中配置位置权限
2. 创建LocationManager类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Windows.Devices.Geolocation; namespace Weather { public class LocationManager { public static async Task<Geoposition> GetPosition() { //请求位置访问权限 var accessStatus = await Geolocator.RequestAccessAsync(); //如果不允许就抛出异常 if (accessStatus != GeolocationAccessStatus.Allowed) throw new Exception(); //实例类 }; //获取设备位置 var position = await geolocator.GetGeopositionAsync(); //返回位置信息 return position; } } }
3.获取位置
var position = await LocationManager.GetPosition(); double lat = position.Coordinate.Point.Position.Latitude; double lon = position.Coordinate.Point.Position.Longitude;