定位

作者: Reliver | 来源:发表于2016-09-14 15:29 被阅读354次

定位

1.实现定位功能

在iOS中使用定位功能,需要导入CoreLocation.h文件,其实现定位功能的步骤如下:

1.创建一个CLLocationManager类型的属性,并且通过懒加载创建出来
//声明定位管理属性
 @property(nonatomic, strong)CLLocationManager * locationManager; 


//通过懒加载创建定位管理对象
- (CLLocationManager *)locationManager{

    if (_locationManager == nil) {
    
     _locationManager = [[CLLocationManager alloc] init];
    }

return _locationManager;
} 
2.在使用定位功能前需要先判断定位服务是否可用
//1.判断定位服务是否可用      
if ([CLLocationManager locationServicesEnabled]) {

    //====定位服务可用====
    //2.设置成总是打开定位服务(8.0以后需要设置)
    //a.先设置plist文件;在inf.plist文件中添加NSLocationAlwaysUsageDescription字段,其对应的类型是字符串,可以设置成任意字符串。如图;
    //b.代码设置成总是打开
    if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
        
        [self.manager requestAlwaysAuthorization];
    }

}else{
    //====定位服务不可用====
    
}
3.设置定位精度
//精度越高,越费电
//kCLLocationAccuracyBest  最高精度
//kCLLocationAccuracyNearestTenMeters; 有10米误差
//kCLLocationAccuracyHundredMeters; 有100米误差
//kCLLocationAccuracyKilometer; 有1000米误差
//kCLLocationAccuracyThreeKilometers; 有3000米误差
[self.manager setDesiredAccuracy:kCLLocationAccuracyBest];
4.自动过滤距离

设置多远距离刷新一次位置信息,单位:米

//让设备每移动10米刷新一次定位信息
[self.manager setDistanceFilter:10];
5.设置定位用途
//CLActivityTypeOther 普通定位
//CLActivityTypeAutomotiveNavigation 汽车导航
//CLActivityTypeFitness 健身
//CLActivityTypeOtherNavigation 其他的运输工具,比如船舶、火车、飞机
[self.manager setActivityType:CLActivityTypeOther];
6.设置代理

通过CLLocationManagerDelegate的协议方法可以获取到定位结果的相关信息

self.manager.delegate = self;
7.开始定位

前面的设置都是定位前的一些设置,并没有真正开启定位功能

//开始定位
[self.locationManager startUpdatingLocation];  
8.区域监听
//经纬度坐标
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(104.06667, 30.66667);
//通过经纬度和半径确定范围
CLRegion * region = [[CLCircularRegion alloc] initWithCenter:coordinate radius:200 identifier:@"天丰利"];
//监听指定范围
[self.manager startMonitoringForRegion:region];
9. CLLocationManagerDelegate协议方法
//位置已经更新的时候调用
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
       fromLocation:(CLLocation *)oldLocation;  
        
//位置已经更新的时候调用(如果实现了这个方法,那么上面那个方法就不会被调用)
- (void)locationManager:(CLLocationManager *)manager
 didUpdateLocations:(NSArray *)locations;  
 
//当定位失败的时候,会调用这个方法  
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error  

//进入监听区域的时候,会调用这个方法
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region;  

//从监听区域出去的时候,会调用这个方法
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region

地址的编码和反编码

iOS专门提供了一个用来通过经纬度获取其对应的地址,和通过地址来获取经纬度的类:CLGeocoder。CLGeocoder声明在<MapKit/MapKit.h>库中。
1.使用CLGeocoder,先创建其对象

//地址解析器
@property (nonatomic, strong) CLGeocoder *geoCoder;
//实例化
self.geoCoder = [[CLGeocoder alloc] init];

2.通过地址解析器将地址解析成经纬度

[self.geocoder geocodeAddressString:address completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {

}

3.通过地址解析器将经纬度反编码成地址

[self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {

}

相关文章

  • 定位,定位,还是定位

    要想成为一个好的文字的输出者,首先要给自己一个合理的定位。定位可以首先想象自己想成为的角色:一个能够通过各种故事启...

  • 10-CSS定位流

    定位流 定位流分类相对定位绝对定位固定定位静态定位 相对定位 什么是相对定位?相对定位就是相对于自己以前在标准流中...

  • CSS中的几种定位

    CSS中常用的定位有 普通定位,相对定位 绝对定位、fixed定位 浮动 1、普通定位和相对定位 css中的元素有...

  • 2017-02-23 CSS 学习笔记

    定位流 定位流分类 相对定位 绝对定位 固定定位 静态定位 什么是相对定位相对定位就是相对于自己以前在标准中的位置...

  • #30天专注橙长计划#品牌定位30(6)营销推广1

    品牌定位的维度包括: 市场定位、价格定位、形象定位、地理定位、人群定位、渠道定位等; 经过前期大量的市场调研工作,...

  • D28:系统地图, 高德地图

    目录一. 定位功能 iPhone手机定位(基站定位, WiFi定位, GPS定位: 定位所需时间递增, 精确度递增...

  • 相对定位、绝对定位和固定定位

    1. 相对定位 定位有三种:相对定位,绝对定位、固定定位。 相对定位: position:relative lef...

  • 定位流

    定位流指的是:相对定位(relative)/绝对定位(absolute)/固定定位(fixed) 不包括静态定位(...

  • CSS补充2

    定位position 定位分为四种:相对定位 relative,绝对定位 absolute,固定定位 fixed,...

  • 定位

    1、static定位(普通流定位) -------------- 默认定位 2、float定位(浮动定位) 例:f...

网友评论

      本文标题:定位

      本文链接:https://www.haomeiwen.com/subject/nmkddttx.html