使用Location Service能協助開發人員為windows Phone 開發具備位置感知(Location-Aware)功能的應用程式。比如很多導航的軟體,尋找附近吃飯、娛樂甚至廁所的應用程式,都是基於這個服務的。
我們有3種方法來擷取裝置的位置。GPS,移動網路基站位置和WiFi位置。下面的圖是這三種方式的優缺點:
需要注意的是:windows phone會根據應用程式的需要選擇一種或者多種方式來確定手機的位置。
三種方式確定位置的優點是有效平衡電池的消耗與位置資訊的準確性。
windows phone 為應用程式提供基於事件(event-driven)的統一介面。
使用地理位置服務的建議:
a. 如果可以的話 使用那個較低準確率的資料來源;
b. 當需要的時候開啟地理位置服務,一旦使用完畢立刻關閉該服務。
- 設定準確率的門限值,減低更新頻率;
- 使用狀態更新事件(StatusChanged)監控服務狀態,提醒使用者狀態的更新;
- 提醒使用者初次啟動地理位置服務時需要等待一段時間(15秒到120秒)。
使用位置服務
- 建立一個GeoCoordinateWatcher對象。
- 建立一個事件處理常式處理使用者位置的改變。
- 在事件觸發時抓取資料。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Device.Location;
using Microsoft.Phone.Tasks;
namespace Day13_LocationServices
{
publicpartialclass MainPage : PhoneApplicationPage
{
GeoCoordinateWatcher gcw;
// Constructor
public MainPage()
{
InitializeComponent();
gcw.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(gcw_PositionChanged);
gcw.Start();
}
void gcw_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
Latitude.Text = e.Position.Location.Latitude.ToString();
Longitude.Text = e.Position.Location.Longitude.ToString();
}
}
}
反應性擴充架構(Reactive Extensions)
- Reactive Extensions能夠輔助應用程式把多種可監控的外來事件轉換成非同步訊息;
- 外來事件包括資料流(data streams),非同步請求(asynchronous requests)和事件(event)等;
- 使用Reactive Extensions,當外部時間觸發的時候,應用程式得到非同步更新訊息(asynchronous requests);
- Reactive Extensions允許應用程式使用查詢(query)操作來對時間進行過濾。
如何使用Reactive Extensions可以參考msdn的這篇文章:http://msdn.microsoft.com/en-us/library/ff637517(VS.92).aspx 參考資料:How to: Get Data from the Location Service for Windows Phone http://msdn.microsoft.com/en-us/library/ff431782(v=vs.92).aspx How to: Use Reactive Extensions to Emulate and Filter Location Data for Windows Phone http://msdn.microsoft.com/en-us/library/ff637517(VS.92).aspx
Windows Phone 7 開發 31 日談——第13日:位置服務
http://www.cnblogs.com/porscheyin/archive/2010/12/23/1914300.html