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,十分简单!

转:iOS-CoreLocation:无论你在哪里,我都要找到你!的更多相关文章

  1. IOS CoreLocation框架的使用(用于地理定位)

    ●  在移动互联网时代,移动app能解决用户的很多生活琐事,比如 ●  导航:去任意陌生的地方 ●  周边:找餐馆.找酒店.找银行.找电影院 ●  在上述应用中,都用到了地图和定位功能,在iOS开发中 ...

  2. iOS直播直播,头都大了

    随着直播市场的火热,市场大军都逐步进入直播市场 ,腾讯旗下的NOW直播也不例外 先说说直播设计底层 一 .流媒体 1 - 伪流媒体 1.1 扫盲:边下载边播放 1.2 伪流媒体:视频不是实时播放的,先 ...

  3. iOS-CoreLocation:无论你在哪里,我都要找到你!

    作者:@翁呀伟呀 授权本站转载 CoreLocation 1.定位 使用步骤: 创建CLLocationManager示例,并且需要强引用它 设置CLLocationManager的代理,监听并获取所 ...

  4. iOS - CoreLocation 定位

    前言 NS_CLASS_AVAILABLE(10_6, 2_0) @interface CLLocationManager : NSObject 1.CoreLocation 定位 配置 1.在 iO ...

  5. iOS与导航相关的都在这

    // 设置导航背景图片 (一旦设置背景图片(变为不透明),透明层不起作用) [self.navigationBar setBackgroundImage:[UIImage imageNamed:@&q ...

  6. 微信收藏了很多语音,有一些比较有意义的,但是发现只能收藏在微信,没有办法导出了,请大神看清楚,是微信【收藏】的语音,ios或者安卓的方法都可以

  7. ios项目里扒出来的json文件

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #000000 } p.p2 { margin: 0.0px 0. ...

  8. Github上关于iOS的各种开源项目集合(强烈建议大家收藏,查看,总有一款你需要)

    下拉刷新 EGOTableViewPullRefresh - 最早的下拉刷新控件. SVPullToRefresh - 下拉刷新控件. MJRefresh - 仅需一行代码就可以为UITableVie ...

  9. iOS及Mac开源项目和学习资料【超级全面】

    UI 下拉刷新 EGOTableViewPullRefresh – 最早的下拉刷新控件. SVPullToRefresh – 下拉刷新控件. MJRefresh – 仅需一行代码就可以为UITable ...

随机推荐

  1. NetFPGA-SUME下reference_nic测试

    Reference_nic Reference_nic是NetFPGA-SUME中提供的一个参考Demo,本文主要介绍如何构建并在SUME上运行reference_nic. GIT源 git clon ...

  2. 带复杂类的list,list<class>前台往后台传输

    1.前台 $("#applyGoods").click(function(){ var usid=$(this).next().text(); var aid=$(this).ne ...

  3. Robot Framework 教程 (5) - 连接Oracel数据库

    Robot Framework 提供了多种Library.其中Database Library可用来连接操作数据库. 1.安装Database Library 打开Robot Framework官网, ...

  4. [华三] IPv6技术白皮书(V1.00)

    IPv6技术白皮书(V1.00) http://www.h3c.com/cn/d_200802/605649_30003_0.htm H3C S7500E IPv6技术白皮书 关键词:IPv6,隧道 ...

  5. [转帖]脑残式网络编程入门(一):跟着动画来学TCP三次握手和四次挥手

    脑残式网络编程入门(一):跟着动画来学TCP三次握手和四次挥手   http://www.52im.net/thread-1729-1-1.html     1.引言 网络编程中TCP协议的三次握手和 ...

  6. php手册 | python手册 | perl手册 | c#.net手册 | c++手册 | ruby手册 | jquery手册 | js手册 | prototype手册 | mysql手册 | smarty手册 | css手册 | html手册 | nginx手册 | apache手册 | shell手册 | svn手册

    收集各种实用类手册: http://shouce.jb51.net/shell/

  7. ySQL性能优化的21个最佳实践 和 mysql使用索引

    MySQL性能优化的21个最佳实践 和 mysql使用索引 今天,数据库的操作越来越成为整个应用的性能瓶颈了,这点对于Web应用尤其明显.关于数据库的性能,这并不只是DBA才需要担心的事,而这更是我 ...

  8. SpringBoot(七)_统一异常处理

    我感觉看了这节课,给我的思考还是很多的,感觉受益良多.废话不多说,一起学习. 统一的 外层结构返回 这样利于代码看着也规范,前端处理也统一 # 错误返回 { "code": 1, ...

  9. ie6 ie7 userdata 本地存储 引发的惨案.

    我使用 documentElement 作为userdata 作为本地存储的载体. document.documentElement.addBehavior("#default#userda ...

  10. tomcat 启动异常 EOFException: Unexpected end of ZLIB input stream

    EVERE: Exception fixing docBase for context [/agdis] java.io .EOFException: Unexpected end of ZLIB i ...