成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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

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

1.定位

使用步驟:

創建CLLocationManager示例,并且需要強引用它

設置CLLocationManager的代理,監聽并獲取所更新的位置

啟動位置更新

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

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

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

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

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

參數分析

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

CLLocation方法的主要參數:

  1. //經緯度 
  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.方向

使用步驟

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

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

參數分析

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

CLHeading方法的主要參數:

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

3.區域監聽

使用步驟

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

  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. //進入范圍時調用 
  2. -?(void)locationManager:(CLLocationManager?*)manager?didEnterRegion:(CLRegion?*)region?{ 
  3. ????NSLog(@"我進來了!"); 
  4. //離開范圍時調用 
  5. -?(void)locationManager:(CLLocationManager?*)manager?didExitRegion:(CLRegion?*)region?{ 
  6. ????NSLog(@"我出去了!"); 

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

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

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

地理編碼

  1. _coder?=?[[CLGeocoder?alloc]?init]; 
  2. [_coder?geocodeAddressString:@"湖北汽車工業學院"?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. }]; 

實現起來比較簡單,關鍵在于如何使用這些數據!

擴展

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

責任編輯:chenqingxiang 來源: 伯恩的遺產的簡書
相關推薦

2016-09-27 19:53:25

IOS 10蘋果

2021-06-04 10:11:07

鴻蒙安卓操作系統

2021-01-28 18:52:57

Kafka副本機制

2020-02-20 08:30:49

OSPF網絡協議路由協議

2021-10-20 09:20:40

手機定位互聯網位置服務

2023-08-29 09:31:01

Scrapy網頁爬蟲

2020-09-11 06:39:29

ThreadLocal線程

2009-10-15 13:48:13

服務器維護常識

2023-08-30 07:39:16

PawSQL數據庫

2024-01-19 07:08:15

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

2021-11-17 11:03:14

Python代碼語法

2023-02-26 23:33:02

SQLMySQL數據庫

2016-01-11 09:48:07

2020-12-17 08:56:51

單例模式JVM

2021-08-05 18:21:29

Autowired代碼spring

2023-04-28 12:37:59

Spring@Bean使用方式

2021-12-22 09:25:14

小程序函數Python

2023-04-23 09:50:50

@BeanSpring

2023-01-13 16:53:17

Annotation底層元注解

2021-04-10 07:04:00

WPS技巧辦公軟件
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 美女一级毛片 | 91一区二区在线观看 | 久久久久国产一区二区三区 | 亚洲精品一区二区三区蜜桃久 | 亚洲精品视频免费 | 精品麻豆剧传媒av国产九九九 | 五月天国产在线 | 色综合天天天天做夜夜夜夜做 | 欧美日韩在线成人 | 一区二区高清不卡 | 精国产品一区二区三区 | 国产日韩欧美一区 | 国产精久久久久久久妇剪断 | 午夜影视 | 欧美一区日韩一区 | 狠狠干网站 | 国产欧美日韩综合精品一区二区 | 懂色中文一区二区三区在线视频 | 九九激情视频 | 97精品超碰一区二区三区 | 亚洲影音先锋 | 久久精品国产一区二区 | 免费观看毛片 | 免费黄色特级片 | 久久网一区二区 | 久久久精品一区二区三区 | 欧美成人精品一区二区男人看 | 国产激情网站 | 国产精品久久久久久久一区探花 | 成人网av | 成人一区二区视频 | 免费成年网站 | 国产成人在线免费 | 色偷偷888欧美精品久久久 | 久久久婷婷 | 九九久久精品视频 | 欧美一区二区三区在线观看视频 | 色久伊人 | 视频一区二区在线观看 | 黄色一级视频免费 | 99视频在线播放 |