文章出處

  1. 先在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.獲取位置

 

var position = await LocationManager.GetPosition();
double lat = position.Coordinate.Point.Position.Latitude; 
double lon = position.Coordinate.Point.Position.Longitude;

 


文章列表




Avast logo

Avast 防毒軟體已檢查此封電子郵件的病毒。
www.avast.com


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()