window phone 獲取經緯度信息
GeoCoordinateWatcher 類提供基于坐標的位置數據,其來自當前的位置提供程序。 來自當前位置提供程序的基于坐標的位置數據,該提供程序是計算機上優先級別最高的位置提供程序,其優先級別取決于一系列因素,如:來自所有提供程序的數據的存在時間和準確性、位置應用程序請求的準確性、與位置提供程序關聯的電量消耗和性能影響。 當前位置提供程序可能會隨時間改變,例如,當 GPS 設備失去內部附屬信號功能并且 Wi-Fi 三邊轉換法提供程序成為計算機上最準確的提供程序時。
若要開始訪問位置數據,請創建 GeoCoordinateWatcher,然后調用 Start 或 TryStart,開始從當前的位置提供程序獲取數據。
可檢查 Status 屬性來確定數據是否可用。 如果數據可用,您可以從 Position 屬性一次獲取位置,或通過處理 PositionChanged 事件接收連續的位置更新。
Permission、Status 以及 Position 屬性支持 INotifyPropertyChanged,因此應用程序可以數據綁定到這些屬性。
在 Windows 7 中,如果位置提供程序已經安裝并能夠解析計算機的位置,則所有 System.Device.Location 類都完全正常。
注意 |
---|
在 Windows 7 Starter 版上,唯一受支持的位置提供商是控制面板中的默認位置提供程序,且必須安裝外接程序以指定緯度和經度。 |
注意:在 Windows 7 之前的 Windows 版本中,以下條件適用:
-
可創建具有構造函數的所有 System.Device.Location 對象,但 Status 屬性將始終具有值 Disabled。
-
不會引發位置事件。
using System; using System.Device.Location;
namespace GetLocationEvent { class Program { static void Main(string[] args) { CLocation myLocation = new CLocation(); myLocation.GetLocationEvent(); Console.WriteLine("Enter any key to quit."); Console.ReadLine();
} class CLocation { GeoCoordinateWatcher watcher;public void GetLocationEvent() { this.watcher = new GeoCoordinateWatcher(); this.watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged); bool started = this.watcher.TryStart(false, TimeSpan.FromMilliseconds(2000)); if (!started) { Console.WriteLine("GeoCoordinateWatcher timed out on start."); } } void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e) { PrintPosition(e.Position.Location.Latitude, e.Position.Location.Longitude); } void PrintPosition(double Latitude, double Longitude) { Console.WriteLine("Latitude: {0}, Longitude {1}", Latitude, Longitude); } } }
}</pre>
</li> </ul>