定位
1.實現(xiàn)定位功能
在iOS中使用定位功能,需要導入CoreLocation.h文件,其實現(xiàn)定位功能的步驟如下:
1.創(chuàng)建一個CLLocationManager類型的屬性,并且通過懶加載創(chuàng)建出來
//聲明定位管理屬性
@property(nonatomic, strong)CLLocationManager * locationManager;
//通過懶加載創(chuàng)建定位管理對象
- (CLLocationManager *)locationManager{
if (_locationManager == nil) {
_locationManager = [[CLLocationManager alloc] init];
}
return _locationManager;
}
2.在使用定位功能前需要先判斷定位服務(wù)是否可用
//1.判斷定位服務(wù)是否可用
if ([CLLocationManager locationServicesEnabled]) {
//====定位服務(wù)可用====
//2.設(shè)置成總是打開定位服務(wù)(8.0以后需要設(shè)置)
//a.先設(shè)置plist文件;在inf.plist文件中添加NSLocationAlwaysUsageDescription字段,其對應(yīng)的類型是字符串,可以設(shè)置成任意字符串。如圖;
//b.代碼設(shè)置成總是打開
if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
[self.manager requestAlwaysAuthorization];
}
}else{
//====定位服務(wù)不可用====
}
3.設(shè)置定位精度
//精度越高,越費電
//kCLLocationAccuracyBest 最高精度
//kCLLocationAccuracyNearestTenMeters; 有10米誤差
//kCLLocationAccuracyHundredMeters; 有100米誤差
//kCLLocationAccuracyKilometer; 有1000米誤差
//kCLLocationAccuracyThreeKilometers; 有3000米誤差
[self.manager setDesiredAccuracy:kCLLocationAccuracyBest];
4.自動過濾距離
設(shè)置多遠距離刷新一次位置信息,單位:米
//讓設(shè)備每移動10米刷新一次定位信息
[self.manager setDistanceFilter:10];
5.設(shè)置定位用途
//CLActivityTypeOther 普通定位
//CLActivityTypeAutomotiveNavigation 汽車導航
//CLActivityTypeFitness 健身
//CLActivityTypeOtherNavigation 其他的運輸工具,比如船舶、火車、飛機
[self.manager setActivityType:CLActivityTypeOther];
6.設(shè)置代理
通過CLLocationManagerDelegate的協(xié)議方法可以獲取到定位結(jié)果的相關(guān)信息
self.manager.delegate = self;
7.開始定位
前面的設(shè)置都是定位前的一些設(shè)置,并沒有真正開啟定位功能
//開始定位
[self.locationManager startUpdatingLocation];
8.區(qū)域監(jiān)聽
//經(jīng)緯度坐標
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(104.06667, 30.66667);
//通過經(jīng)緯度和半徑確定范圍
CLRegion * region = [[CLCircularRegion alloc] initWithCenter:coordinate radius:200 identifier:@"天豐利"];
//監(jiān)聽指定范圍
[self.manager startMonitoringForRegion:region];
9. CLLocationManagerDelegate協(xié)議方法
//位置已經(jīng)更新的時候調(diào)用
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation;
//位置已經(jīng)更新的時候調(diào)用(如果實現(xiàn)了這個方法,那么上面那個方法就不會被調(diào)用)
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations;
//當定位失敗的時候,會調(diào)用這個方法
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
//進入監(jiān)聽區(qū)域的時候,會調(diào)用這個方法
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region;
//從監(jiān)聽區(qū)域出去的時候,會調(diào)用這個方法
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
地址的編碼和反編碼
iOS專門提供了一個用來通過經(jīng)緯度獲取其對應(yīng)的地址,和通過地址來獲取經(jīng)緯度的類:CLGeocoder。CLGeocoder聲明在<MapKit/MapKit.h>庫中。
1.使用CLGeocoder,先創(chuàng)建其對象
//地址解析器
@property (nonatomic, strong) CLGeocoder *geoCoder;
//實例化
self.geoCoder = [[CLGeocoder alloc] init];
2.通過地址解析器將地址解析成經(jīng)緯度
[self.geocoder geocodeAddressString:address completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
}
3.通過地址解析器將經(jīng)緯度反編碼成地址
[self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
}