2. 創建LocationManager類 3.獲取位置 ...
- 先在Package.appxmanifest中配置位置許可權
2. 創建LocationManager類
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using Windows.Devices.Geolocation; 7 8 namespace Weather 9 { 10 public class LocationManager 11 { 12 public static async Task<Geoposition> GetPosition() 13 { 14 //請求位置訪問許可權 15 var accessStatus = await Geolocator.RequestAccessAsync(); 16 //如果不允許就拋出異常 17 if (accessStatus != GeolocationAccessStatus.Allowed) throw new Exception(); 18 //實例類 19 var geolocator = new Geolocator { DesiredAccuracyInMeters = 0 }; 20 //獲取設備位置 21 var position = await geolocator.GetGeopositionAsync(); 22 //返回位置信息 23 return position; 24 } 25 } 26 }
3.獲取位置
1 var position = await LocationManager.GetPosition(); 2 double lat = position.Coordinate.Point.Position.Latitude; 3 double lon = position.Coordinate.Point.Position.Longitude;