一、介绍
1、定位使用CoreLocation框架
2、主要功能
(1)基础定位
(2)地理编码反编码
3、IOS8 IOS9之后的改变
(1)需要在info.plist里面添加定位服务的目的:
1⃣️NSLocationAlwaysUsageDescription
2⃣️NSLocationWhenInUseUsageDescription
⭐️注意:如果忘记写 就不能使用定位功能 没有任何定位信息
(2)请求用户授权
1⃣️requestAlwaysAuthorization
2⃣️requestWhenInUseAuthorization
⭐️注意:如果和描述的目的不匹配 也不能使用定位功能
- NSLocationAlwaysUsageDescription对应requestAlwaysAuthorization
- NSLocationWhenInUseUsageDescription对应requestWhenInUseAuthorization
(3)IOS9 按HOME键进入后台 如果需要继续定位 就要:
1> 在info.plist文件里面添加:
Required background modes -> App registers for location updates
如果没有添加这对键值 却使用后台定位功能 会直接崩溃
2> allowsBackgroundLocationUpdates 属性需要设置成YES
二、使用
1、使用定位服务所需的相关类 和其他的数据类型
(1)CLLocationManager
定位的管理者 可以通过这个类创建定位服务的功能
(2)CLLocation
地理位置信息相关的一个类
1⃣️coordinate:经纬度
latitude:纬度
longitude:经度
2⃣️altitude:高度
3⃣️horizontalAccuracy:水平的精准度 可以用它来监测是否定位成功 如果是正数一定定位成功
4⃣️verticalAccuracy:垂直的精准度
🈚️speed :速度
(3)CLLocationCoordinate2D
坐标的数据类型(结构体)
(4)CLRegion
表示范围的一个类
(5)CLGeocoder
地理编码 反编码的一个类
(6)CLPlacemark
表示地标的一个类 用文字表示出来位置信息的类(里面同时包含了location)
(7)CLHeading
表示导航方向的一个类 航向
2、具体使用
(1)定位
0⃣️检查用户是否在设置中打开了定位服务
1⃣️初始化定位对象:
2⃣️info中添加描述使用定位的目的 并向用户申请授权
3⃣️挂上代理 实现代理方法
4⃣️如果需要使用后台定位服务的功能 需要在info.plist文件里面添加:
Required background modes -> App registers for location updates
🈚️开始定位
(2)地理编码 反编码
地理编解码 在编解码的时候是一个耗时的操作 可以使用异步操作
- 1、地理编码:把地名转换成位置信息 用处:把文字描述的位置转换成地图上的经纬度
- 2、反地理编码:把位置信息转换成文字 用处:可以通过点击选择地图上的某一位置来获得这一位置文字的描述
distanceFilter 多少米更新一次
desiredAccuracy 设置定位的精准度
代码示例
//
// ViewController.m
// 定位
//
// Created by scsys on 16/3/7.
// Copyright © 2016年 安静SRR. All rights reserved.
//
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()<CLLocationManagerDelegate>
{
CLLocationManager *locationManager;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//0、 判断用户是否在设置里面打开了定位服务功能
if ( ![CLLocationManager locationServicesEnabled]) {
//1.跳出弹出框 提示用户打开步骤
//2.通过代码调到设置页面
#pragma mark ------1跳出弹出框 提示用户打开步骤
// UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"请在设置中打开定位服务功能" preferredStyle:UIAlertControllerStyleAlert];
// UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"👌" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//
// }];
// [alert addAction:action1];
//
// [self presentViewController:alert animated:YES completion:nil];
#pragma mark ----------2通过代码跳到设置页面
//openURL:用于跳转APP 跳到IOS允许跳到的界面
if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]) {
//跳转到设置界面
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}
return;
}
//1、创建定位管理者的对象 一般都用属性去创建
locationManager = [[CLLocationManager alloc]init];
//设置多少米去更新一次位置信息
locationManager.distanceFilter = 100;
//设置定位的精准度
locationManager.desiredAccuracy =
kCLLocationAccuracyBest;
//2.info中添加描述使用定位的目的 并向用户申请授权
[locationManager requestWhenInUseAuthorization];
//3、挂上代理 并实现代理方法
locationManager.delegate = self;
//4.如果需要使用后台定位服务的功能 需要在info.plist文件里面添加:Required background modes -> App registers for location updates
locationManager.allowsBackgroundLocationUpdates = YES;
//5.开始定位
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray<CLLocation *> *)locations{
CLLocation *curloc = [locations firstObject];
//坐标:经纬度 结构体
/* latitude:纬度
longitude:经度
*/
CLLocationCoordinate2D coordinte = curloc.coordinate;
NSLog(@"经度%f纬度%f",coordinte.longitude,coordinte.latitude);
// CLLocationDistance 是double类型
NSLog(@"高度是%f",curloc.altitude);
//可以通过水平精准度来判断是否定位成功 如果是负数表示定位错误 如果是正数表示定位成功
NSLog(@"水平精准度是%f",curloc.horizontalAccuracy);
NSLog(@"垂直精准度是%f",curloc.verticalAccuracy);
/*
course当前设备前进的方向
0°表示向北
90°表示向东
180°表示向南
270°表示向西
*/
NSLog(@"航向是%f",curloc.course);
NSLog(@"当前行驶的速度速度是%f",curloc.speed);
NSLog(@"楼层的高度是%ld层",curloc.floor.level);
//当前定位的日期NSData
NSLog(@"当前的时间是%@层",curloc.timestamp);
//1、是否超速
//2.行驶距离:总距离 = 每一次更新位置得到的距离累加
// CLLocationDistance 距离
//- (CLLocationDistance)getDistanceFrom:(const CLLocation *)location得到两次loction之间距离的方法
//3.行驶时间:总时间 = 每一次更新得到的时间间隔的累加
//可以得到当前的时间戳
//日期有一个方法 计算两个日期之间的间隔
//通过记录上一次和更新的Location得到上一次时间
//4.平均速度:总距离/总时间
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error{
NSLog(@"定位错误");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
地理编解码的代码示例放下一章,看着应该会比较清晰。