iOS定位CoreLocation的使用

CoreLocation
1.定位
使用步骤:
创建CLLocationManager示例,并且需要强引用它
设置CLLocationManager的代理,监听并获取所更新的位置
启动位置更新

_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方法的主要参数:

 //经纬度@property(readonly, nonatomic) CLLocationCoordinate2D coordinate;
//海平面@property(readonly, nonatomic) CLLocationDistance altitude;
//速度@property(readonly, nonatomic) CLLocationSpeed speed
//当前时间戳@property(readonly, nonatomic, copy) NSDate *timestamp;

2.方向
使用步骤
和定位一样的三个步骤,不同的是获取方向不需要授权

_manager = [[CLLocationManager alloc] init];_manager.delegate = self;            
[_manager startUpdatingHeading];

参数分析
在Manager的代理方法locationManager: didUpdateHeading中,其传入的newHeading参数是CLHeading类型。
CLHeading方法的主要参数:
//与磁北方向的偏角@property(readonly, nonatomic) CLLocationDirection magneticHeading; //与正北方向的偏角@property(readonly, nonatomic) CLLocationDirection trueHeading;

3.区域监听
使用步骤
也需要大致三个步骤,其中前两个步骤和定位一样,第三个步骤是创建一个范围:

  _ 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];

代理方法(一进一出)

//进入范围时调用
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region { NSLog(@"我进来了!");}
//离开范围时调用
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region { NSLog(@"我出去了!");}

4.地理编码 & 反地理编码
所谓地理编码就是你给他一个地名,它返回给你此地的经纬度等信息;反地理编码就是你给他一个经纬度,它返回给你一个地名。如果没用到定位功能就不需要授权。
地理编码

    _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
中有很多可用的属性,大家可以进去看看。
反地理编码

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 出自http://my.oschina.net/are1OfBlog/blog/420034 摘要 现在很多社交、...
    JJO阅读 9,619评论 4 19
  • 简介 在移动互联网时代,移动app能解决用户的很多生活琐事,比如 周边:找餐馆、找KTV、找电影院等等 导航:根据...
    JonesCxy阅读 5,136评论 1 1
  • CoreLocation框架 一. iOS8.0之前的定位(✨✨✨✨✨) 前台定位导入CoreLocation框架...
    尼古拉斯赵四爷阅读 4,649评论 0 2
  • 说正事吧,讲讲穿搭, 如何让衣服整体搭配完美, 就好比图片上的这一套,短款西装可以搭配七分或者九分阔腿裤显个高腿长...
    ParisHe倩倩阅读 1,287评论 0 0
  • 作为一个读者 想读懂你的温柔 想写下你的悲伤 离开家的时候带不走的是你 床上洒满的金色阳光 舍不得的是一中的杨柳 ...
    小羊的海阅读 3,427评论 0 2