iOS-CoreLocation:無論你在哪裡,我都要找到你!

來源:互聯網
上載者:User

標籤:

@翁呀偉呀 授權本站轉載

CoreLocation

1.定位

使用步驟:

  • 建立CLLocationManager樣本,並且需要強引用它

  • 設定CLLocationManager的代理,監聽並擷取所更新的位置

  • 啟動位置更新

123 _manager?=?[[CLLocationManager?alloc]?init];_manager.delegate?=?self;[_manager?startUpdatingLocation];

由於在iOS8中,需要開發人員主動向系統請求授權,所以在iOS8及以上系統中,需要以下步驟:

  • 在info.plist檔案中設定NSLocationWhenInUseUsageDescription或NSLocationAlwaysUsageDescription

  • 在代碼中使用[_manager requestWhenInUseAuthorization]請求授權

  • 實現Manager的代理方法didChangeAuthorizationStatus:,根據狀態判斷是否啟動位置更新

參數分析

在Manager的代理方法locationManager: didUpdateLocations:中,其傳入的locations參數是CLLocation類型。

CLLocation方法的主要參數:

12345678 //經緯度@property(readonly,?nonatomic)?CLLocationCoordinate2D?coordinate;//海平面@property(readonly,?nonatomic)?CLLocationDistance?altitude;//速度@property(readonly,?nonatomic)?CLLocationSpeed?speed//目前時間戳@property(readonly,?nonatomic,?copy)?NSDate?*timestamp;

2.方向

使用步驟

和定位一樣的三個步驟,不同的是擷取方向不需要授權

123 _manager?=?[[CLLocationManager?alloc]?init];_manager.delegate?=?self;[_manager?startUpdatingHeading];

參數分析

在Manager的代理方法locationManager: didUpdateHeading:中,其傳入的newHeading參數是CLHeading類型。

CLHeading方法的主要參數:

1234 //與磁北方向的偏角@property(readonly,?nonatomic)?CLLocationDirection?magneticHeading;//與正北方向的偏角@property(readonly,?nonatomic)?CLLocationDirection?trueHeading;

3.地區監聽

使用步驟

也需要大致三個步驟,其中前兩個步驟和定位一樣,第三個步驟是建立一個範圍:

12345678 _manager?=?[[CLLocationManager?alloc]?init];_manager.delegate?=?self;if?([[UIDevice?currentDevice].systemVersion?doubleValue]?>=?8.0)?{???[_manager?requestAlwaysAuthorization];}CLLocationCoordinate2D?coordinate?=?CLLocationCoordinate2DMake(32.656688,?110.74677);CLCircularRegion?*circular?=?[[CLCircularRegion?alloc]?initWithCenter:coordinate?radius:1000?identifier:@"bourne"];[_manager?startMonitoringForRegion:circular];

代理方法(一進一出)

12345678 //進入範圍時調用-?(void)locationManager:(CLLocationManager?*)manager?didEnterRegion:(CLRegion?*)region?{????NSLog(@"我進來了!");}//離開範圍時調用-?(void)locationManager:(CLLocationManager?*)manager?didExitRegion:(CLRegion?*)region?{????NSLog(@"我出去了!");}

HELP:在iOS8.3中好像沒作用,真機和模擬器都不行,iOS7.1正常工作!我也不知道怎麼回事兒,如果有人知道希望能告訴我一下。謝謝。

4.地理編碼 & 反地理編碼

所謂地理編碼就是你給他一個地名,它返回給你此地的經緯度等資訊;反地理編碼就是你給他一個經緯度,它返回給你一個地名。如果沒用到定位功能就不需要授權。

地理編碼

12345 _coder?=?[[CLGeocoder?alloc]?init];[_coder?geocodeAddressString:@"湖北汽車工業學院"?completionHandler:^(NSArray?*placemarks,?NSError?*error)?{???CLPlacemark?*marks?=?placemarks.firstObject;???NSLog(@"%f?-?%f",?marks.location.coordinate.latitude,?marks.location.coordinate.longitude);}];

CLPlacemark中有很多可用的屬性,大家可以進去看看。

反地理編碼

123456 CLLocation?*loc?=?[[CLLocation?alloc]?initWithLatitude:32.656688?longitude:110.74677];[_coder?reverseGeocodeLocation:loc?completionHandler:^(NSArray?*placemarks,?NSError?*error)?{???for?(CLPlacemark?*mark?in?placemarks)?{???????NSLog(@"%@",?mark.name);???}}];

實現起來比較簡單,關鍵在於如何使用這些資料!

擴充

CoreLocation使用起來還是比較麻煩的,需要授權,判斷系統版本等等,所以一邊推薦使用第三方架構,比如:LocationManager就很不錯,使用Block,十分簡單!

  

iOS-CoreLocation:無論你在哪裡,我都要找到你!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.