關於Core Location-ios定位

來源:互聯網
上載者:User

標籤:des   style   io   os   使用   ar   資料   sp   div   

IOS中的core location提供了定位功能,能定位裝置的當前座標,同一時候能得到裝置移動資訊。由於對定位裝置的輪詢是非常耗電的,所以最好僅僅在非常必要的前提下啟動。

當中,最重要的類是CLLocationManager,定位管理。

其定位有3種方式:

1,GPS,最精確的定位方式,貌似iphone1是不支援的。

2,蜂窩基站三角定位,這樣的定位在訊號基站比較秘籍的城市比較準確。

3,Wifi,這樣的方式貌似是通過網路電訊廠商的資料庫得到的資料,在3種定位種最不精確


使用方式:

1,引入CoreLocation的包,一般的預設範本裡是沒有的,所以須要手動匯入。

2,通過啟動CLLocationManager來啟動定位服務,由於定位資訊是須要輪詢的,並且對於程式來說是須要一定時間才會得到的,所以翠玉lcationManager的操作大多都給託付來完畢。

載入locationManager的代碼:

    CLLocationManager *locationManager = [[CLLocationManager alloc] init];//建立位置管理器     locationManager.delegate=self;    locationManager.desiredAccuracy=kCLLocationAccuracyBest;    locationManager.distanceFilter=1000.0f;    //啟動位置更新    [locationManager startUpdatingLocation];
desiredAccuracy為設定定位的精度,能夠設為最優,裝置會自己主動用最精確的方式去定位。

distanceFilter是距離過濾器,為了降低對定位裝置的輪詢次數,位置的改變不會每次都去通知託付,而是在移動了足夠的距離時才通知託付程式,它的單位是米,這裡設定為至少移動1000再通知託付處理更新。

startUpdatingLocation就是啟動定位管理了,一般來說,在不須要更新定位時最好關閉它,用stopUpdatingLocation,能夠節省電量。


對於託付CLLocationManagerDelegate,最經常使用的方法是:

- (void)locationManager:(CLLocationManager *)manager   
    didUpdateToLocation:(CLLocation *)newLocation   
           fromLocation:(CLLocation *)oldLocation;

這種方法即定位改變時託付會啟動並執行方法。

能夠得到新位置,舊位置,CLLocation裡面有經度緯度的座標值,

同一時候CLLocation還有個屬性horizontalAccuracy,用來得到水平上的準確度,它的大小就是定位精度的半徑,單位為米。

假設值為-1,則說明此定位不可信。


另外託付另一個經常用法是

- (void)locationManager:(CLLocationManager *)manager   
       didFailWithError:(NSError *)error ;

當定位出現錯誤時就會調用這種方法。


關於Core Location-ios定位

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.