一、介紹
1、定位使用CoreLocation框架
2、主要功能
(1)基礎(chǔ)定位
(2)地理編碼反編碼
3、IOS8 IOS9之后的改變
(1)需要在info.plist里面添加定位服務(wù)的目的:
1??NSLocationAlwaysUsageDescription
2??NSLocationWhenInUseUsageDescription
??注意:如果忘記寫 就不能使用定位功能 沒(méi)有任何定位信息
(2)請(qǐng)求用戶授權(quán)
1??requestAlwaysAuthorization
2??requestWhenInUseAuthorization
??注意:如果和描述的目的不匹配 也不能使用定位功能
- NSLocationAlwaysUsageDescription對(duì)應(yīng)requestAlwaysAuthorization
- NSLocationWhenInUseUsageDescription對(duì)應(yīng)requestWhenInUseAuthorization
(3)IOS9 按HOME鍵進(jìn)入后臺(tái) 如果需要繼續(xù)定位 就要:
1> 在info.plist文件里面添加:
Required background modes -> App registers for location updates
如果沒(méi)有添加這對(duì)鍵值 卻使用后臺(tái)定位功能 會(huì)直接崩潰
2> allowsBackgroundLocationUpdates 屬性需要設(shè)置成YES
二岳守、使用
1、使用定位服務(wù)所需的相關(guān)類 和其他的數(shù)據(jù)類型
(1)CLLocationManager
定位的管理者 可以通過(guò)這個(gè)類創(chuàng)建定位服務(wù)的功能
(2)CLLocation
地理位置信息相關(guān)的一個(gè)類
1??coordinate:經(jīng)緯度
latitude:緯度
longitude:經(jīng)度
2??altitude:高度
3??horizontalAccuracy:水平的精準(zhǔn)度 可以用它來(lái)監(jiān)測(cè)是否定位成功 如果是正數(shù)一定定位成功
4??verticalAccuracy:垂直的精準(zhǔn)度
???speed :速度
(3)CLLocationCoordinate2D
坐標(biāo)的數(shù)據(jù)類型(結(jié)構(gòu)體)
(4)CLRegion
表示范圍的一個(gè)類
(5)CLGeocoder
地理編碼 反編碼的一個(gè)類
(6)CLPlacemark
表示地標(biāo)的一個(gè)類 用文字表示出來(lái)位置信息的類(里面同時(shí)包含了location)
(7)CLHeading
表示導(dǎo)航方向的一個(gè)類 航向
2碌冶、具體使用
(1)定位
0??檢查用戶是否在設(shè)置中打開了定位服務(wù)
1??初始化定位對(duì)象:
2??info中添加描述使用定位的目的 并向用戶申請(qǐng)授權(quán)
3??掛上代理 實(shí)現(xiàn)代理方法
4??如果需要使用后臺(tái)定位服務(wù)的功能 需要在info.plist文件里面添加:
Required background modes -> App registers for location updates
???開始定位
(2)地理編碼 反編碼
地理編解碼 在編解碼的時(shí)候是一個(gè)耗時(shí)的操作 可以使用異步操作
- 1湿痢、地理編碼:把地名轉(zhuǎn)換成位置信息 用處:把文字描述的位置轉(zhuǎn)換成地圖上的經(jīng)緯度
- 2、反地理編碼:把位置信息轉(zhuǎn)換成文字 用處:可以通過(guò)點(diǎn)擊選擇地圖上的某一位置來(lái)獲得這一位置文字的描述
distanceFilter 多少米更新一次
desiredAccuracy 設(shè)置定位的精準(zhǔn)度
代碼示例
//
// 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种樱、 判斷用戶是否在設(shè)置里面打開了定位服務(wù)功能
if ( ![CLLocationManager locationServicesEnabled]) {
//1.跳出彈出框 提示用戶打開步驟
//2.通過(guò)代碼調(diào)到設(shè)置頁(yè)面
#pragma mark ------1跳出彈出框 提示用戶打開步驟
// UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"溫馨提示" message:@"請(qǐng)?jiān)谠O(shè)置中打開定位服務(wù)功能" 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通過(guò)代碼跳到設(shè)置頁(yè)面
//openURL:用于跳轉(zhuǎn)APP 跳到IOS允許跳到的界面
if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]) {
//跳轉(zhuǎn)到設(shè)置界面
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}
return;
}
//1蒙袍、創(chuàng)建定位管理者的對(duì)象 一般都用屬性去創(chuàng)建
locationManager = [[CLLocationManager alloc]init];
//設(shè)置多少米去更新一次位置信息
locationManager.distanceFilter = 100;
//設(shè)置定位的精準(zhǔn)度
locationManager.desiredAccuracy =
kCLLocationAccuracyBest;
//2.info中添加描述使用定位的目的 并向用戶申請(qǐng)授權(quán)
[locationManager requestWhenInUseAuthorization];
//3俊卤、掛上代理 并實(shí)現(xiàn)代理方法
locationManager.delegate = self;
//4.如果需要使用后臺(tái)定位服務(wù)的功能 需要在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];
//坐標(biāo):經(jīng)緯度 結(jié)構(gòu)體
/* latitude:緯度
longitude:經(jīng)度
*/
CLLocationCoordinate2D coordinte = curloc.coordinate;
NSLog(@"經(jīng)度%f緯度%f",coordinte.longitude,coordinte.latitude);
// CLLocationDistance 是double類型
NSLog(@"高度是%f",curloc.altitude);
//可以通過(guò)水平精準(zhǔn)度來(lái)判斷是否定位成功 如果是負(fù)數(shù)表示定位錯(cuò)誤 如果是正數(shù)表示定位成功
NSLog(@"水平精準(zhǔn)度是%f",curloc.horizontalAccuracy);
NSLog(@"垂直精準(zhǔn)度是%f",curloc.verticalAccuracy);
/*
course當(dāng)前設(shè)備前進(jìn)的方向
0°表示向北
90°表示向東
180°表示向南
270°表示向西
*/
NSLog(@"航向是%f",curloc.course);
NSLog(@"當(dāng)前行駛的速度速度是%f",curloc.speed);
NSLog(@"樓層的高度是%ld層",curloc.floor.level);
//當(dāng)前定位的日期NSData
NSLog(@"當(dāng)前的時(shí)間是%@層",curloc.timestamp);
//1、是否超速
//2.行駛距離:總距離 = 每一次更新位置得到的距離累加
// CLLocationDistance 距離
//- (CLLocationDistance)getDistanceFrom:(const CLLocation *)location得到兩次loction之間距離的方法
//3.行駛時(shí)間:總時(shí)間 = 每一次更新得到的時(shí)間間隔的累加
//可以得到當(dāng)前的時(shí)間戳
//日期有一個(gè)方法 計(jì)算兩個(gè)日期之間的間隔
//通過(guò)記錄上一次和更新的Location得到上一次時(shí)間
//4.平均速度:總距離/總時(shí)間
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error{
NSLog(@"定位錯(cuò)誤");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
地理編解碼的代碼示例放下一章害幅,看著應(yīng)該會(huì)比較清晰消恍。