導(dǎo)語:
公司的項(xiàng)目中用到了百度地圖的相關(guān)功能(定位、導(dǎo)航责静、自定義大頭針)其中封裝了幾個(gè)百度地圖的工具類袁滥,方便開發(fā)使用。
1.百度地位基礎(chǔ)知識(shí)
由于系統(tǒng)原因灾螃,iOS不允許使用第三方定位题翻,因此地圖SDK中的定位方法,本質(zhì)上是對(duì)原生定位的二次封裝腰鬼。通過封裝嵌赠,開發(fā)者可更便捷的使用。此外熄赡,地圖SDK中還提供了相應(yīng)的定位圖層(支持定位三態(tài)效果)姜挺,幫助開發(fā)者顯示當(dāng)前位置信息。
2.具體實(shí)現(xiàn)代碼
創(chuàng)建一個(gè)定位工具管理類
.h文件
#import <Foundation/Foundation.h>
//定義一個(gè)block彼硫,定位成功后返回經(jīng)緯度
typedef void(^LocationBlock)(NSString *lat,NSString *lon);
@interface BDLocationManager : NSObject
///當(dāng)前位置的緯度
@property(nonatomic,copy)NSString *lat;
///當(dāng)前位置的經(jīng)度
@property(nonatomic,copy)NSString *lon;
///當(dāng)前位置的地址信息
@property(nonatomic,copy)NSString *address;
+(instancetype)sharedManager;
///獲取當(dāng)前位置經(jīng)緯度
-(void)getGps:(LocationBlock)block;
@end
.m文件
#import "BDLocationManager.h"
#import <BaiduMapAPI_Location/BMKLocationService.h>
#import <CoreLocation/CoreLocation.h>
#import <BaiduMapAPI_Search/BMKSearchComponent.h>
@interface BDLocationManager ()<BMKLocationServiceDelegate,UIAlertViewDelegate,BMKGeoCodeSearchDelegate>
///定義block
@property(nonatomic,copy)LocationBlock block;
/**
* 定位管理
*/
@property(nonatomic,strong)BMKLocationService *locService;
/**
* 地理編碼
*/
@property(nonatomic,strong) BMKGeoCodeSearch *geoCode;
@end
@implementation BDLocationManager
+ (instancetype)sharedManager {
static BDLocationManager *_instance = nil;
static dispatch_once_t oncetoken;
dispatch_once(&oncetoken, ^{
_instance = [[BDLocationManager alloc] init];
});
return _instance;
}
- (instancetype)init {
if (self = [super init]) {
//初始化BMKLocationService
_locService = [[BMKLocationService alloc]init];
//設(shè)置定位精度
[_locService setDesiredAccuracy:kCLLocationAccuracyBest];
_locService.delegate = self;
_locService.distanceFilter = 30.f;
if ([CLLocationManager locationServicesEnabled]) {
NSLog(@"定位服務(wù)可用");
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
if ([CLLocationManager authorizationStatus]==kCLAuthorizationStatusAuthorizedWhenInUse||[CLLocationManager authorizationStatus]==kCLAuthorizationStatusAuthorizedAlways) {
NSLog(@"用戶授權(quán)");
} else if(status == kCLAuthorizationStatusDenied) {
//定位服務(wù)開啟 --但是用戶沒有允許他定位
NSLog(@"用戶未授權(quán),跳轉(zhuǎn)設(shè)置界面");
UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"您的定位未經(jīng)授權(quán)" message:@"PGS需要根據(jù)您的位置獲取信息" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"去設(shè)置", nil];
alertView.delegate=self;
[alertView show];
}
} else {
NSLog(@"去開啟定位服務(wù)");
}
}
return self;
}
//獲取當(dāng)前的地理位置
- (void)getGps:(LocationBlock)block {
self.block = block;
//啟動(dòng)LocationService
[_locService startUserLocationService];
}
//處理位置坐標(biāo)更新
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation {
//獲取到位置信息 賦值
[BDLocationManager sharedManager].lat = [@(userLocation.location.coordinate.latitude) stringValue];
[BDLocationManager sharedManager].lon = [@(userLocation.location.coordinate.longitude) stringValue];
self.block([@(userLocation.location.coordinate.latitude) stringValue],[@(userLocation.location.coordinate.longitude) stringValue]);
//初始化反地址編碼選項(xiàng)(數(shù)據(jù)模型)
BMKReverseGeoCodeOption *option = [[BMKReverseGeoCodeOption alloc] init];
//將TextField中的數(shù)據(jù)傳到反地址編碼模型
option.reverseGeoPoint= CLLocationCoordinate2DMake(userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude);
//調(diào)用反地址編碼方法炊豪,讓其在代理方法中輸出
if ([self.geoCode reverseGeoCode:option]) {
NSLog(@"反檢索發(fā)送成功");
} else {
NSLog(@"反檢索發(fā)送失敗");
}
//關(guān)閉定位服務(wù)
// [_locService stopUserLocationService];
}
/**
*定位失敗后,會(huì)調(diào)用此函數(shù)
*@param error 錯(cuò)誤號(hào)
*/
- (void)didFailToLocateUserWithError:(NSError *)error {
NSLog(@"定位失敗");
}
#pragma mark geoCode的Get方法乌助,實(shí)現(xiàn)延時(shí)加載
- (BMKGeoCodeSearch *)geoCode {
if(!_geoCode) {
_geoCode= [[BMKGeoCodeSearch alloc] init];
_geoCode.delegate= self;
}
return _geoCode;
}
/**
*返回反地理編碼搜索結(jié)果
*@param searcher 搜索對(duì)象
*@param result 搜索結(jié)果
*@param error 錯(cuò)誤號(hào)溜在,@see BMKSearchErrorCode
*/
- (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error {
//BMKReverseGeoCodeResult是編碼的結(jié)果,包括地理位置他托,道路名稱掖肋,uid,城市名等信息
if (error == BMK_SEARCH_NO_ERROR) {
//在此處理正常結(jié)果
[BDLocationManager sharedManager].address = result.address;
NSLog(@"位置:%@",[BDLocationManager sharedManager].address);
} else {
NSLog(@"抱歉赏参,未找到結(jié)果");
}
}
//實(shí)現(xiàn)相關(guān)delegate 處理位置信息更新
//處理方向變更信息
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation {
// NSLog(@"處理方向變更信息 heading is %@",userLocation.heading);
}
#pragma mark 跳轉(zhuǎn)設(shè)置界面
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if([[UIApplication sharedApplication] canOpenURL:url]) {
NSURL*url =[NSURL URLWithString:UIApplicationOpenSettingsURLString];
[[UIApplication sharedApplication] openURL:url];
}
}
}
@end