2014年7月9日 星期三

台灣繁中CLLocationManager Class Reference

CLLocationManager Class Reference

Inherits from
Conforms to
Framework
/System/Library/Frameworks/CoreLocation.framework
Availability
Available in iOS 2.0 and later.
Companion guide
Declared in
CLLocation.h
CLLocationManager.h
Related sample code


總覧
The CLLocationManager class 定義一個界面讓你可以去設定和"地點移點"和"方向改變"相關的事件。你要用一個CLLocationManager的實體來設定何時要接受 "地點移點"和"方向改變"的事件,並可以接收地點和方向的相關資料。
地點管理物件提供下列支援:
  • 追踪使用者的目前位置,並可以調整GPS的精確度。
  • 報告目前方向計的方向. (iOS only)
  • 監控你想監控的區域,並回報進入和離開的資訊.
  • 當app進入背景時,會延遲傳送地點更新. (iOS 6 and later only)
  • 報告出附近的beacons的範圍.
有些定位服務需要在iDevice上特定的硬體,例如方向資訊必需要有方向計,CLLocationManager class定義了一些方法讓你可以去得知這些服務在你的裝置上可否使用。

關於要使用這種服務,你應該先設定跟精準度有關的參數,因為裝置會為了省電而主動的關掉不需要用到的硬體。舉例來說,把精準度設定為一公里,可以給location manager一個彈性範圍去決定是否關掉GPS定位而直接採用WiFi或手機訊息,因為關閉GPS能明顯的省電。
設定和使用CLLocationManager 物件來傳遞事件:
  1. 永遠記得要在使用該服務前先檢查該裝置是否有執行該服務,如果沒有就不要使用它。
  2. 建立一個the CLLocationManager class的實體.
  3. 指派你的物件給delegate去呼叫,這個物件必須符合CLLocationManagerDelegate 協定.
  4. 設定每個你想到使用的service的相關properties。
  5. 呼叫你想啟用的service的啟用method。
所以地點和方向的更新都會傳送給你指派的delegate物件,這些delegate的callback你可以參考 CLLocationManagerDelegate Protocol Reference.

取得使用者目前的定位
設定地點相關的服務有二種選擇:
  • 使用標準定位服務,它允許你設定想要的地理資訊精確度,當地點更新時會接到訊息。標準定位服務可以在iOS的所有版本上使用、和10.6以上的OSX上使用。
  • 需求事件只在地點有明顯的改變時發生作用,它提供少數幾個追踪的選擇,但卻提供大量省電方案和在你的app未執行時仍能更新地點資訊的能力。這項服務只在iOS4.0以上的版本才有提供,而且裝置必需能用3G的功能。
要啟用標準定位服務,你要呼叫startUpdatingLocation 方法. 這項服務適合用在需要精細地點更新資訊的App上。具體來說,這項服務把 desiredAccuracy 和 distanceFilter 這兩個property加入決定是否傳送更新資訊的考量。 導航app和其他定期更新位置的app都需要用精確的標準定位服務。然而,這項服務需要一直開著定位追踪的硬體,這會造成耗電量大增。
如果你的app不需要定期更新地點資訊,你可以考慮使用startMonitoringSignificantLocationChanges 方法來啟用定位事件的傳送。這個方法更適用於當你只需要先定義初始位置,之後要在明確的位置改變時,再傳送下一次的更新訊息。這個介面只在它偵測到3G基地台改變時才發出訊息。
不論你是用那一種定位服務,位置的資料都會透過相關的delegate回傳給你。因為它在初始化位置時會花上幾秒鐘的時間,所以通常location manager會立刻先丟給你它上一次cache下來的位置資料,等它初始化完成之後才會傳送當下的即時資料。因此,最好先確定位置資料的timestamp是你要的之後,再拿來使用。如果有二個location services被同步啟用,那它們會把資料都傳進相同的delegate實作方式之中。
在iOS 6之後,你可以把在進入背景之後的位置資料都延遲處理,最後再一次傳送給delegate方法,建議當你的app可以一次處理先前的資料時,就使用這個機制,例如app要追踪登山者的路線,就可以先不用使用app,等過了一定距離再一次計算,這可以讓你的app更加的省電。

Using Regions to Monitor Boundary Crossings

In iOS 4.0 and later and OS X 10.8 and later, you can use the region-monitoring service to define the boundaries for multiple geographical regions. After registering a region using the startMonitoringForRegion: method, the location manager tracks movement across the region’s boundary and reports that movement to its delegate. You might use region monitoring to alert the user to approaching landmarks or to provide other relevant information. For example, upon approaching a dry cleaners, an application could notify the user to pick up any clothes that had been dropped off and are now ready.
In iOS, the regions you register with the location manager persist between launches of your application. If a region crossing occurs while your iOS app is not running, the system automatically wakes it up (or relaunches it) in the background so that it can process the event. When relaunched, all of the regions you configured previously are made available in the monitoredRegions property of any location manager objects you create.
In OS X, region monitoring works only while the app is running (either in the foreground or background) and the user’s system is awake. The system does not launch apps to deliver region-related notifications. Similarly, if the user puts the computer to sleep, the system does not deliver region monitoring notifications to your app. If the user wakes up the computer inside a monitored region, the system does deliver region notifications to your app if it is running. However, if the computer enters and exits the region before being woken up, no notification is delivered.


The region monitoring service operates independently of any location services in use by your application, and you may use it in conjunction with any of the other services. Region monitoring is not supported on all devices. Use theregionMonitoringAvailable class method to determine if region monitoring can be used.

Configuring Heading-Related Services

In iOS, a device with the appropriate hardware may also report heading information. When the value in theheadingAvailable property is YES, you can use a location manager object to retrieve heading information. To begin the delivery of heading-related events, assign a delegate to the location manager object and call the location manager’s startUpdatingHeading method. If location updates are also enabled, the location manager returns both the true heading and magnetic heading values. If location updates are not enabled, the location manager returns only the magnetic heading value. These features are not available in OS X.

沒有留言:

張貼留言