在手機(jī)上獲取的地理位置信息分為兩類:
- 當(dāng)前位置的經(jīng)緯度信息
- 當(dāng)前位置根據(jù)經(jīng)緯度逆編碼后的地理名稱
一锐极、概述
通過CoreLocation.framework(以下簡稱CL)蓬抄,會使用GPS践盼、基站他去、wifi方式浮还、藍(lán)牙、磁力計(jì)张抄、氣壓計(jì)獲取當(dāng)前地理位置砂蔽、方位洼怔、海拔署惯。由于CoreLocation是系統(tǒng)的定位框架,所以通過獲取的經(jīng)緯度進(jìn)行逆編碼后的地理名稱的準(zhǔn)確度還是很高的镣隶。
現(xiàn)在很多團(tuán)購軟件极谊,對用戶的位置具有強(qiáng)依賴性诡右,像美團(tuán)、美團(tuán)外賣轻猖、餓了么帆吻、百度外賣等,需要根據(jù)所處位置推薦商家和推送信息咙边。那么位置的準(zhǔn)確性就非常影響用戶的體驗(yàn)了猜煮,現(xiàn)在市面上主流的地圖:百度地圖、高德地圖败许、騰訊地圖都具有逆編碼的能力王带,集成SDK后對手機(jī)采集的經(jīng)緯度信息進(jìn)行處理就能得到地理名稱。
如下圖:
demo地址
二市殷、框架概述
-
CLLocationManager
:用來啟動或停止定位服務(wù)
//常用方法
//判斷定位服務(wù)是否開啟
+ (BOOL)locationServicesEnabled __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_4_0);
//獲取定位服務(wù)授權(quán)狀態(tài)
+ (CLAuthorizationStatus)authorizationStatus __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_4_2);
//請求使用時(shí)位置授權(quán) 每次定位前需要調(diào)用
- (void)requestWhenInUseAuthorization __OSX_AVAILABLE_STARTING(__MAC_NA, __IPHONE_8_0);
//請求始終使用位置授權(quán) 每次定位前需要調(diào)用
- (void)requestAlwaysAuthorization __OSX_AVAILABLE_STARTING(__MAC_NA, __IPHONE_8_0) __TVOS_PROHIBITED;
//開啟位置更新愕撰,CLLocationManager通過代理將位置異步傳給代理對象
- (void)startUpdatingLocation __TVOS_PROHIBITED;
//停止位置更新
- (void)stopUpdatingLocation;
//請求一次位置更新
- (void)requestLocation __OSX_AVAILABLE_STARTING(__MAC_NA, __IPHONE_9_0);
//常用屬性
//最小更新位置的間隔距離
@property(assign, nonatomic) CLLocationDistance distanceFilter;
//定位精度
@property(assign, nonatomic) CLLocationAccuracy desiredAccuracy;
//允許后臺位置更新
@property(assign, nonatomic) BOOL allowsBackgroundLocationUpdates __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_9_0) __TVOS_PROHIBITED __WATCHOS_PROHIBITED;
//最新接收到的位置
@property(readonly, nonatomic, copy, nullable) CLLocation *location;
- CLLocationManagerDelegate : CLLocationManager的代理
//位置產(chǎn)生更新,從舊位置變到新位置
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_6, __MAC_NA, __IPHONE_2_0, __IPHONE_6_0) __TVOS_PROHIBITED __WATCHOS_PROHIBITED;
//位置產(chǎn)生更新
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray<CLLocation *> *)locations __OSX_AVAILABLE_STARTING(__MAC_10_9,__IPHONE_6_0);
//CLLocationManager產(chǎn)生錯(cuò)誤
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error;
- CLLocation : 位置對象醋寝,由CLLocationManager產(chǎn)生
//CLLocation中包含位置的經(jīng)度和緯度
struct CLLocationCoordinate2D {
CLLocationDegrees latitude;
CLLocationDegrees longitude;
};
typedef struct CLLocationCoordinate2D CLLocationCoordinate2D;
//代表位置的定位精度
typedef double CLLocationDistance;
//各類經(jīng)度
extern const CLLocationAccuracy kCLLocationAccuracyBestForNavigation __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_4_0);
extern const CLLocationAccuracy kCLLocationAccuracyBest;
extern const CLLocationAccuracy kCLLocationAccuracyNearestTenMeters;
extern const CLLocationAccuracy kCLLocationAccuracyHundredMeters;
extern const CLLocationAccuracy kCLLocationAccuracyKilometer;
extern const CLLocationAccuracy kCLLocationAccuracyThreeKilometers;
//海拔
@property(readonly, nonatomic) CLLocationDistance altitude;
//水平精度
@property(readonly, nonatomic) CLLocationAccuracy horizontalAccuracy;
//垂直精度
@property(readonly, nonatomic) CLLocationAccuracy verticalAccuracy;
//速度
@property(readonly, nonatomic) CLLocationSpeed speed __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_2_2) __TVOS_PROHIBITED;
//時(shí)間戳
@property(readonly, nonatomic, copy) NSDate *timestamp;
//通過給定的經(jīng)緯度初始化一個(gè)CLLocation對象
- (instancetype)initWithLatitude:(CLLocationDegrees)latitude
longitude:(CLLocationDegrees)longitude;
//通過經(jīng)緯度搞挣、水平精度、垂直精度音羞、時(shí)間戳初始化一個(gè)CLLocation對象
- (instancetype)initWithCoordinate:(CLLocationCoordinate2D)coordinate
altitude:(CLLocationDistance)altitude
horizontalAccuracy:(CLLocationAccuracy)hAccuracy
verticalAccuracy:(CLLocationAccuracy)vAccuracy
timestamp:(NSDate *)timestamp;
//通過經(jīng)緯度囱桨、水平精度、垂直精度嗅绰、速度蝇摸、時(shí)間戳初始化一個(gè)CLLocation對象
- (instancetype)initWithCoordinate:(CLLocationCoordinate2D)coordinate
altitude:(CLLocationDistance)altitude
horizontalAccuracy:(CLLocationAccuracy)hAccuracy
verticalAccuracy:(CLLocationAccuracy)vAccuracy
course:(CLLocationDirection)course
speed:(CLLocationSpeed)speed
timestamp:(NSDate *)timestamp __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_4_2);
//返回兩個(gè)位置間的間距
- (CLLocationDistance)distanceFromLocation:(const CLLocation *)location __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_3_2);
- CLError : 定位所產(chǎn)生的錯(cuò)誤對象
typedef NS_ENUM(NSInteger, CLError) {
kCLErrorLocationUnknown = 0, // 位置錯(cuò)誤類型,但是CL框架會嘗試定位
kCLErrorDenied, // 用戶拒絕定位權(quán)限
kCLErrorNetwork, // 網(wǎng)絡(luò)錯(cuò)誤
kCLErrorHeadingFailure, // heading 沒有被決定
kCLErrorRegionMonitoringDenied, // 用戶拒絕位置監(jiān)視
kCLErrorRegionMonitoringFailure, // 無法監(jiān)視注冊的區(qū)域
kCLErrorRegionMonitoringSetupDelayed, // CL無法立刻初始化區(qū)域監(jiān)視器
kCLErrorRegionMonitoringResponseDelayed, // 地理圍欄將會被傳遞办陷,但不會立刻出現(xiàn)
kCLErrorGeocodeFoundNoResult, // A geocode request yielded no result
kCLErrorGeocodeFoundPartialResult, // A geocode request yielded a partial result
kCLErrorGeocodeCanceled, // A geocode request was cancelled
kCLErrorDeferredFailed, // Deferred mode failed
kCLErrorDeferredNotUpdatingLocation, // Deferred mode failed because location updates disabled or paused
kCLErrorDeferredAccuracyTooLow, // Deferred mode not supported for the requested accuracy
kCLErrorDeferredDistanceFiltered, // Deferred mode does not support distance filters
kCLErrorDeferredCanceled, // Deferred mode request canceled a previous request
kCLErrorRangingUnavailable, // Ranging cannot be performed
kCLErrorRangingFailure, // General ranging failure
};
三貌夕、使用CL的定位步驟
- 請求定位服務(wù)權(quán)限
- 使用authorizationStatus
的類方法來判斷是否得到了用戶的授權(quán) - 如果授權(quán)狀態(tài)是kCLAuthorizationStatusRestricted
或 kCLAuthorizationStatusDenied
代表應(yīng)用沒有權(quán)限使用定位,應(yīng)當(dāng)立即終止使用定位服務(wù) - 如果授權(quán)狀態(tài)是kCLAuthorizationStatusNotDetermined
代表應(yīng)用還未得到明確的授權(quán)民镜,需要調(diào)用 requestWhenInUseAuthorization
或 requestAlwaysAuthorization
方法來向用戶申請授權(quán)
- 使用authorizationStatus
- 判斷定位服務(wù)是否可用
- 調(diào)用CLLocationManager 對象的 + (BOOL)locationServicesEnabled 方法獲取定位服務(wù)是否可以使用啡专。
- 初始化CLLocationManager對象
- 對CLLocationManager對象進(jìn)行強(qiáng)引用,以防被ARC釋放制圈,因?yàn)镃LLocationManager對象進(jìn)行定位時(shí)是異步的们童,所以,需要通過回調(diào)來接收定位后的結(jié)果
- 實(shí)現(xiàn) CLLocationManagerDelegate 代理
四鲸鹦、注意事項(xiàng)
- 在應(yīng)用的Info.plist文件中加入NSLocationWhenInUseUsageDescription來解釋使用位置服務(wù)的用途慧库。
- 調(diào)用requestWhenInUseAuthorization
或 requestAlwaysAuthorization
方法來通知系統(tǒng)向用戶請求授權(quán)。(在初次使用時(shí)會彈出馋嗜,用戶拒絕后只能去設(shè)置中手動開啟)
Tip
如果忘記以上兩步齐板,會導(dǎo)致授權(quán)請求立刻失效,應(yīng)用無法使用定位服務(wù)。
- requestWhenInUseAuthorization是在應(yīng)用使用期間授權(quán)甘磨,應(yīng)用進(jìn)入后臺后不再能使用定位服務(wù)橡羞。
- requestAlwaysAuthorization 是始終使用定位服務(wù),無論應(yīng)用在前臺還是在后臺济舆。
- 以上兩種權(quán)限可以都寫在Info.plist文件中卿泽,系統(tǒng)會默認(rèn)是使用期間授權(quán)。
五滋觉、代碼實(shí)現(xiàn)
調(diào)用HFLocation的getLocationName:方法签夭,就會通過回調(diào)返回一個(gè)手機(jī)所處的位置逆解碼地理位置。
//
// HFLocation.h
// HFLocation
//
// Created by whf on 17/6/25.
// Copyright ? 2017年 apple. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface HFLocation : NSObject
- (instancetype)sharedInstance;
- (void)getLocationName:(void(^) (NSError *error,NSDictionary *result))block;
@end
//
// HFLocation.m
// HFLocation
//
// Created by whf on 17/6/25.
// Copyright ? 2017年 apple. All rights reserved.
//
#import "HFLocation.h"
#import <CoreLocation/CoreLocation.h>
@interface HFLocation() <CLLocationManagerDelegate>
@property (nonatomic, strong) CLLocationManager *locationMgr;
@property (nonatomic, copy) void (^block) (NSError *error,NSDictionary *result);
@end
@implementation HFLocation
- (instancetype)sharedInstance {
static HFLocation *location = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
location = [[HFLocation alloc] init];
});
return location;
}
- (instancetype)init {
if (self = [super init]) {
CLLocationManager *locationMgr = [[CLLocationManager alloc] init];
self.locationMgr = locationMgr;
[locationMgr requestWhenInUseAuthorization];
locationMgr.desiredAccuracy = kCLLocationAccuracyBest;
locationMgr.distanceFilter = 10.0;
locationMgr.delegate = self;
}
return self;
}
- (void)dealloc {
NSLog(@"%@ -dealloc",NSStringFromClass([self class]));
}
- (void)getLocationName:(void (^)(NSError *, NSDictionary *))block {
if ([CLLocationManager locationServicesEnabled]) {//定位服務(wù)開啟
[self.locationMgr startUpdatingLocation];
self.block = block;
}
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray<CLLocation *> *)locations{
if (locations) {
CLLocation *location = [locations firstObject];
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
__weak typeof(self) weak_self = self;
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
__strong typeof(weak_self) self = weak_self;
if (error) {
NSLog(@"%@",error);
if (self.block) {
self.block(error,nil);
}
}
else {
CLPlacemark *placemark = [placemarks firstObject];
self.block(nil,placemark.addressDictionary);
}
}];
[self.locationMgr stopUpdatingLocation];
}
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error {
NSLog(@"%@",error);
}
@end