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

鸿蒙安卓操作系统

2020-02-20 08:30:49

OSPF网络协议路由协议

2021-01-28 18:52:57

Kafka副本机制

2021-10-20 09:20:40

手机定位互联网位置服务

2024-01-19 07:08:15

PowerShell自定义变量变量输出方式

2023-08-30 07:39:16

PawSQL数据库

2009-10-15 13:48:13

服务器维护常识

2020-09-11 06:39:29

ThreadLocal线程

2023-08-29 09:31:01

Scrapy网页爬虫

2020-12-17 08:56:51

单例模式JVM

2016-01-11 09:48:07

2021-11-17 11:03:14

Python代码语法

2021-08-05 18:21:29

Autowired代码spring

2023-04-28 12:37:59

Spring@Bean使用方式

2023-02-26 23:33:02

SQLMySQL数据库

2023-04-23 09:50:50

@BeanSpring

2021-12-22 09:25:14

小程序函数Python

2023-01-13 16:53:17

Annotation底层元注解

2021-04-10 07:04:00

WPS技巧办公软件
点赞
收藏

51CTO技术栈公众号