自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

iOS-CoreLocation:你在哪我都知道!

移動開發(fā) iOS
在Manager的代理方法locationManager: didUpdateLocations:中,其傳入的locations參數(shù)是CLLocation類型。

1.定位

使用步驟:

創(chuàng)建CLLocationManager示例,并且需要強引用它

設置CLLocationManager的代理,監(jiān)聽并獲取所更新的位置

啟動位置更新

  1. _manager?=?[[CLLocationManager?alloc]?init]; 
  2. _manager.delegate?=?self; 
  3. [_manager?startUpdatingLocation]; 

由于在iOS8中,需要開發(fā)者主動向系統(tǒng)請求授權(quán),所以在iOS8及以上系統(tǒng)中,需要以下步驟:

在info.plist文件中設置NSLocationWhenInUseUsageDescription或NSLocationAlwaysUsageDescription

在代碼中使用[_manager requestWhenInUseAuthorization]請求授權(quán)

實現(xiàn)Manager的代理方法didChangeAuthorizationStatus:,根據(jù)狀態(tài)判斷是否啟動位置更新

參數(shù)分析

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

CLLocation方法的主要參數(shù):

  1. //經(jīng)緯度 
  2. @property(readonly,?nonatomic)?CLLocationCoordinate2D?coordinate; 
  3. //海平面 
  4. @property(readonly,?nonatomic)?CLLocationDistance?altitude; 
  5. //速度 
  6. @property(readonly,?nonatomic)?CLLocationSpeed?speed 
  7. //當前時間戳 
  8. @property(readonly,?nonatomic,?copy)?NSDate?*timestamp; 

2.方向

使用步驟

和定位一樣的三個步驟,不同的是獲取方向不需要授權(quán)

  1. _manager?=?[[CLLocationManager?alloc]?init]; 
  2. _manager.delegate?=?self; 
  3. [_manager?startUpdatingHeading]; 

參數(shù)分析

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

CLHeading方法的主要參數(shù):

  1. //與磁北方向的偏角 
  2. @property(readonly,?nonatomic)?CLLocationDirection?magneticHeading; 
  3. //與正北方向的偏角 
  4. @property(readonly,?nonatomic)?CLLocationDirection?trueHeading; 

3.區(qū)域監(jiān)聽

使用步驟

也需要大致三個步驟,其中前兩個步驟和定位一樣,第三個步驟是創(chuàng)建一個范圍:

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

代理方法(一進一出)

  1. //進入范圍時調(diào)用 
  2. -?(void)locationManager:(CLLocationManager?*)manager?didEnterRegion:(CLRegion?*)region?{ 
  3. ????NSLog(@"我進來了!"); 
  4. //離開范圍時調(diào)用 
  5. -?(void)locationManager:(CLLocationManager?*)manager?didExitRegion:(CLRegion?*)region?{ 
  6. ????NSLog(@"我出去了!"); 

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

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

所謂地理編碼就是你給他一個地名,它返回給你此地的經(jīng)緯度等信息;反地理編碼就是你給他一個經(jīng)緯度,它返回給你一個地名。如果沒用到定位功能就不需要授權(quán)。

地理編碼

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

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

反地理編碼

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

實現(xiàn)起來比較簡單,關鍵在于如何使用這些數(shù)據(jù)!

擴展

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

責任編輯:chenqingxiang 來源: 伯恩的遺產(chǎn)的簡書
相關推薦

2016-09-27 19:53:25

IOS 10蘋果

2021-06-04 10:11:07

鴻蒙安卓操作系統(tǒng)

2020-02-20 08:30:49

OSPF網(wǎng)絡協(xié)議路由協(xié)議

2021-01-28 18:52:57

Kafka副本機制

2021-10-20 09:20:40

手機定位互聯(lián)網(wǎng)位置服務

2024-01-19 07:08:15

PowerShell自定義變量變量輸出方式

2023-08-30 07:39:16

PawSQL數(shù)據(jù)庫

2020-09-11 06:39:29

ThreadLocal線程

2023-08-29 09:31:01

Scrapy網(wǎng)頁爬蟲

2009-10-15 13:48:13

服務器維護常識

2021-11-17 11:03:14

Python代碼語法

2023-02-26 23:33:02

SQLMySQL數(shù)據(jù)庫

2021-08-05 18:21:29

Autowired代碼spring

2016-01-11 09:48:07

2023-04-28 12:37:59

Spring@Bean使用方式

2020-12-17 08:56:51

單例模式JVM

2021-12-22 09:25:14

小程序函數(shù)Python

2023-04-23 09:50:50

@BeanSpring

2023-01-13 16:53:17

Annotation底層元注解

2021-04-10 07:04:00

WPS技巧辦公軟件
點贊
收藏

51CTO技術棧公眾號