(GeekBand) iOS高级编程第二周--地图相关

iOS8.0之前的定位

前台定位

导入CoreLocation框架和对应的主头文件
#import <CoreLocation/CoreLocation.h> 
        
创建CLLcationManager对象,并设置代理

_locationM = [[CLLocationManager alloc] init];
        _locationM.delegate = self;
    
调用CLLcationManager对象的startUpdatingLocation方法进行更新用户位置
[_locationM startUpdatingLocation];
        
实现代理方法,接收位置参数
-(void)locationManager:(nonnull CLLocationManager *)manager didUpdateLocations:(nonnull NSArray<CLLocation *> *)locations

后台定位

在前台定位基础上,勾选后台模式update locations(如下图)

Paste_Image.png

添加后台运行模式

iOS8.0+ 至 iOS9.0之前的定位

前台定位

导入CoreLocation框架和对应的主头文件
#import <CoreLocation/CoreLocation.h>
    
创建CLLcationManager对象,并设置代理 请求前台定位授权,并配置KEY
_locationM = [[CLLocationManager alloc] init];
        _locationM.delegate = self;
        if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) 
        {
        [_locationM requestWhenInUseAuthorization];
        }
Paste_Image.png
调用CLLcationManager对象的startUpdatingLocation方法进行更新用户位置
[_locationM startUpdatingLocation];
        
实现代理方法,接收位置参数
-(void)locationManager:(nonnull CLLocationManager *)manager didUpdateLocations:(nonnull NSArray<CLLocation *> *)locations

后台定位

方案一:在APP处于前台定位授权场景下,勾选后台运行模式update locations (如下图)
添加后台运行模式

Paste_Image.png

方案二:请求前后台定位授权,并配置KEY
[_locationM requestAlwaysAuthorization];

Paste_Image.png

各种授权状态讲解

switch (status) {
            // 用户还未决定
        case kCLAuthorizationStatusNotDetermined:
        {
            NSLog(@"用户还未决定");
            break;
        }
            // 问受限
        case kCLAuthorizationStatusRestricted:
        {
            NSLog(@"访问受限");
            break;
        }
            // 定位关闭时和对此APP授权为never时调用
        case kCLAuthorizationStatusDenied:
        {
            // 定位是否可用(是否支持定位或者定位是否开启)
            if([CLLocationManager locationServicesEnabled])
            {
                NSLog(@"定位开启,但被拒");
            }else
            {
                NSLog(@"定位关闭,不可用");
            }
            break;
        }
            // 获取前后台定位授权
        case kCLAuthorizationStatusAuthorizedAlways:
//        case kCLAuthorizationStatusAuthorized: // 失效,不建议使用
        {
            NSLog(@"获取前后台定位授权");
            break;
        }
            // 获得前台定位授权
        case kCLAuthorizationStatusAuthorizedWhenInUse:
        {
            NSLog(@"获得前台定位授权");
            break;
        }
        default:
            break;
    }

iOS9.0 定位补充

前台定位

导入CoreLocation框架和对应的主头文件
#import <CoreLocation/CoreLocation.h>
        
创建CLLcationManager对象,并设置代理 请求前台定位授权,并配置KEY
_locationM = [[CLLocationManager alloc] init];
        _locationM.delegate = self;
        if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) 
        {
        [_locationM requestWhenInUseAuthorization];
        }

Paste_Image.png

调用CLLcationManager对象的startUpdatingLocation方法进行更新用户位置
[_locationM startUpdatingLocation];
        
实现代理方法,接收位置参数
-(void)locationManager:(nonnull CLLocationManager *)manager didUpdateLocations:(nonnull NSArray<CLLocation *> *)locations
        
后台定位

方案一:在APP处于前台定位授权场景下,勾选后台运行模式update locations (如下图) 并且,调用以下方法,设置允许后台定位
if ([[UIDevice currentDevice].systemVersion floatValue] >= 9.0) {
        _locationM.allowsBackgroundLocationUpdates = YES;
        }
Paste_Image.png

添加后台运行模式
方案二:请求前后台定位授权,并配置KEY

[_locationM requestAlwaysAuthorization];
        

补充

作用:按照定位精确度从低到高进行排序,逐个进行定位。如果获取到的位置不是精确度最高的那个,也会在定位超时后,通过代理告诉外界
注意:
一个要实现代理的定位失败方法;
二:不能与startUpdatingLocation同时使用
[_locationM requestLocation];
CLLocation对象详解

coordinate (当前位置所在的经纬度)
altitude (海拔)
speed (当前速度)
-distanceFromLocation: (获取两个位置之间的直线物理距离)
指南针

导入CoreLocation框架和对应的主头文件
#import <CoreLocation/CoreLocation.h> 
        
创建CLLcationManager对象,并设置代理
_locationM = [[CLLocationManager alloc] init];
        _locationM.delegate = self;
        
调用CLLcationManager对象的startUpdatingHeading方法进行更新设备朝向
[_locationM startUpdatingHeading];
        
实现代理方法,获取方向参数,根据方向参数旋转图片
-(void)locationManager:(nonnull CLLocationManager *)manager didUpdateHeading:(nonnull CLHeading *)newHeading
        {
            // 获取当前设备朝向(磁北方向)
            CGFloat angle = newHeading.magneticHeading;

            // 转换成为弧度
            CGFloat radian = angle / 180.0 * M_PI;

            // 反向旋转指南针
            [UIView animateWithDuration:0.5 animations:^{
                self.compassView.transform = CGAffineTransformMakeRotation(-radian);
            }];
        }

注意: 获取用户的设备朝向,不需要用户进行定位授权
区域监听

导入CoreLocation框架和对应的主头文件
#import <CoreLocation/CoreLocation.h>
        
创建CLLcationManager对象,并设置代理,请求授权(iOS8.0之后才需要) 请求前后台定位授权,并配置KEY
_locationM = [[CLLocationManager alloc] init];
        _locationM.delegate = self;
        if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) 
        {
        [_locationM requestAlwaysAuthorization];
        }
        

调用CLLcationManager对象的startMonitoringForRegion:方法进行监听指定区域
    // 创建区域中心
            CLLocationCoordinate2D center = CLLocationCoordinate2DMake(29.12345, 131.23456);
            // 创建区域(指定区域中心,和区域半径)
            CLCircularRegion *region = [[CLCircularRegion alloc] initWithCenter:center radius:1000 identifier:@"小码哥"];
            // 开始监听指定区域
            [self.locationM startMonitoringForRegion:region];
        
实现代理方法,获取区域进入或者离开状态
// 进去监听区域后调用(调用一次)
        -(void)locationManager:(nonnull CLLocationManager *)manager didEnterRegion:(nonnull CLRegion *)region
        {
            NSLog(@"进入区域---%@", region.identifier);
            [manager stopMonitoringForRegion:region];
        }

        // 离开监听区域后调用(调用一次)
        -(void)locationManager:(nonnull CLLocationManager *)manager didExitRegion:(nonnull CLRegion *)region
        {
            NSLog(@"离开区域---%@", region.identifier);
        }

(反)地理编码


导入CoreLocation框架和对应的主头文件
#import <CoreLocation/CoreLocation.h>
        
创建CLGeocoder对象
_geoC = [[CLGeocoder alloc] init];
        
地理编码
方案1:
// 地理编码方案一:直接根据地址进行地理编码(返回结果可能有多个,因为一个地点有重名)
            [self.geoC geocodeAddressString:@"北京天安门" completionHandler:^(NSArray<CLPlacemark *> * __nullable placemarks, NSError * __nullable error) {
                // 包含区,街道等信息的地标对象
                CLPlacemark *placemark = [placemarks firstObject];
                // 城市名称
        //        NSString *city = placemark.locality;
                // 街道名称
        //        NSString *street = placemark.thoroughfare;
                // 全称
                NSString *name = placemark.name;
                self.addressDetailTV.text = [NSString stringWithFormat:@"%@", name];
                self.latitudeTF.text = [NSString stringWithFormat:@"%f", placemark.location.coordinate.latitude];
                self.longtitudeTF.text = [NSString stringWithFormat:@"%f", placemark.location.coordinate.longitude];
            }];
        
方案2:
    // 地理编码方案二:根据地址和指定区域两个条件进行地理编码(更加精确)
            [self.geoC geocodeAddressString:@"北京" inRegion:nil completionHandler:^(NSArray<CLPlacemark *> * __nullable placemarks, NSError * __nullable error) {
                // 包含区,街道等信息的地标对象
                CLPlacemark *placemark = [placemarks firstObject];
                self.addressDetailTV.text = placemark.description;
                self.latitudeTF.text = [NSString stringWithFormat:@"%f", placemark.location.coordinate.latitude];
                self.longtitudeTF.text = [NSString stringWithFormat:@"%f", placemark.location.coordinate.longitude];
            }];
        
反地理编码
 // 创建CLLocation对象
            CLLocation *location = [[CLLocation alloc] initWithLatitude:23.132931 longitude:113.375924;
            // 根据CLLocation对象进行反地理编码
            [self.geoC reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * __nullable placemarks, NSError * __nullable error) {
                // 包含区,街道等信息的地标对象
                CLPlacemark *placemark = [placemarks firstObject];
                // 城市名称
        //        NSString *city = placemark.locality;
                // 街道名称
        //        NSString *street = placemark.thoroughfare;
                // 全称
                NSString *name = placemark.name;
                self.addressDetailTV.text = [NSString stringWithFormat:@"%@", name];
            }];

定位的第三方框架

下载框架(locationManager框架)
locationManager框架 下载地址
导入框架(直接拖入项目)
框架功能说明
可以使用block进行获取用户位置
可以设置超时时长
可以取消和强制终止定位
按照github中,该框架的readme参照使用

满地打滚卖萌求赞,如果本文帮助到你,轻点下方的红心,给作者君增加更新的动力。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 196,099评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,473评论 2 373
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 143,229评论 0 325
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,570评论 1 267
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,427评论 5 358
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,335评论 1 273
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,737评论 3 386
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,392评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,693评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,730评论 2 312
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,512评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,349评论 3 314
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,750评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,017评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,290评论 1 251
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,706评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,904评论 2 335

推荐阅读更多精彩内容