转:iOS-CoreLocation:无论你在哪里,我都要找到你!
1.定位
使用步驟:
創建CLLocationManager示例,并且需要強引用它
設置CLLocationManager的代理,監聽并獲取所更新的位置
啟動位置更新
| 1 2 3 | _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方法的主要參數:
| 1 2 3 4 5 6 7 8 | //經緯度 @property(readonly,?nonatomic)?CLLocationCoordinate2D?coordinate; //海平面 @property(readonly,?nonatomic)?CLLocationDistance?altitude; //速度 @property(readonly,?nonatomic)?CLLocationSpeed?speed //當前時間戳 @property(readonly,?nonatomic,?copy)?NSDate?*timestamp; |
2.方向
使用步驟
和定位一樣的三個步驟,不同的是獲取方向不需要授權
| 1 2 3 | _manager?=?[[CLLocationManager?alloc]?init]; _manager.delegate?=?self; [_manager?startUpdatingHeading]; |
參數分析
在Manager的代理方法locationManager: didUpdateHeading:中,其傳入的newHeading參數是CLHeading類型。
CLHeading方法的主要參數:
| 1 2 3 4 | //與磁北方向的偏角 @property(readonly,?nonatomic)?CLLocationDirection?magneticHeading; //與正北方向的偏角 @property(readonly,?nonatomic)?CLLocationDirection?trueHeading; |
3.區域監聽
使用步驟
也需要大致三個步驟,其中前兩個步驟和定位一樣,第三個步驟是創建一個范圍:
| 1 2 3 4 5 6 7 8 | _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]; |
代理方法(一進一出)
| 1 2 3 4 5 6 7 8 | //進入范圍時調用 -?(void)locationManager:(CLLocationManager?*)manager?didEnterRegion:(CLRegion?*)region?{ ????NSLog(@"我進來了!"); } //離開范圍時調用 -?(void)locationManager:(CLLocationManager?*)manager?didExitRegion:(CLRegion?*)region?{ ????NSLog(@"我出去了!"); } |
HELP:在iOS8.3中好像沒作用,真機和模擬器都不行,iOS7.1正常工作!我也不知道怎么回事兒,如果有人知道希望能告訴我一下。謝謝。
4.地理編碼 & 反地理編碼
所謂地理編碼就是你給他一個地名,它返回給你此地的經緯度等信息;反地理編碼就是你給他一個經緯度,它返回給你一個地名。如果沒用到定位功能就不需要授權。
地理編碼
| 1 2 3 4 5 | _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中有很多可用的屬性,大家可以進去看看。
反地理編碼
| 1 2 3 4 5 6 | 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,十分簡單!
轉載于:https://www.cnblogs.com/guoxiaoqian/p/4784241.html
總結
以上是生活随笔為你收集整理的转:iOS-CoreLocation:无论你在哪里,我都要找到你!的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: BZOJ3924 : [Zjoi2015
- 下一篇: BZOJ 2588: Spoj 1062