今天和大家分享:集成百度地圖iOS SDK,實現在線建議查詢地址。這是我的github源碼,使用oc純代碼編寫,部署版本是ios8,大家可以下載運行代碼;
一、配置開發環境
進入百度地圖開發者中心根據步驟即可實現。我是使用CocoaPods導入地圖SDK(沒有安裝CocoaPods,可以根據這篇文章使用CocoaPods安裝和使用教程),而且我也極力推薦大家使用CocoaPods來管理第三方庫;
我主要講一下注意點:
注意一:務必在info.plist文件配置
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
注意二:按需引入SDK中的頭文件,不要把所有的頭文件包含進來,這樣會導致編譯時間延長,如下實現在線建議查詢地址我這項目中只需要引入連個頭文件即可
#import <BaiduMapAPI_Search/BMKSearchComponent.h> //引入檢索功能所有的頭文件
#import <BaiduMapAPI_Location/BMKLocationComponent.h>//引入定位功能所有的頭文件
注意三: info.plist文件記得添加另外兩個key-value,
- 打開定位服務
*需要在info.plist文件中添加(以下二選一,兩個都添加默認使用NSLocationWhenInUseUsageDescription):
*NSLocationWhenInUseUsageDescription 允許在前臺使用時獲取GPS的描述
*NSLocationAlwaysUsageDescription 允許永遠可獲取GPS的描述
- 添加Bundle display name
care.png
二、代碼實現
先簡要介紹下項目的各個類,如下圖
introduction.png
主要介紹下LocationManager
類是對開始定位和停止定位封裝好的單例,主要有三個方法:
/**
* 開始定位
*/
- (void)startLocation;
/**
* 停止定位
*/
- (void)stopLocation;
/**
* 判斷是否授權開啟定位
*/
- (BOOL)isAuthorizeOpenLocation;
1、在AppDelegate.m文件中
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 要使用百度地圖,請先啟動BaiduMapManager
_mapManager = [[BMKMapManager alloc]init];
BOOL ret = [_mapManager start:BMAppKey generalDelegate:self];
if (!ret) {
NSLog(@"manager start failed!");
}
// 程序一啟動開始定位
[[LocationManager shareLocationManager] startLocation];
// 1.初始化窗口
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
// 2.設置窗口根控制器
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[[SelectAddressTableViewController alloc] init]];
// 3.顯示窗口
[self.window makeKeyAndVisible];
return YES;
}
2、LocationManager中的startLocation方法中獲取位置信息
//初始化BMKLocationService
- (instancetype)init {
if (self = [super init]) {
//初始化BMKLocationService
_locService = [[BMKLocationService alloc]init];
_locService.delegate = self;
// 設定定位的最小更新距離(米),更新頻率。默認為kCLDistanceFilterNone
_locService.distanceFilter = 100.0f;
// 設定定位精度。默認為kCLLocationAccuracyBest。
_locService.desiredAccuracy = kCLLocationAccuracyNearestTenMeters; // 定位精度10m
_geoSearcher = [[BMKGeoCodeSearch alloc] init];
_geoSearcher.delegate = self;
}
return self;
}
- (void)startLocation {
if ([self isAuthorizeOpenLocation]) { // 已經授權定位
//啟動LocationService
[_locService startUserLocationService];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"定位服務未開啟" message:@"請在設置中開啟定位服務" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"去設置", nil];
[alert show];
}
}
// 判斷是否授權開啟定位
- (BOOL)isAuthorizeOpenLocation {
if ([CLLocationManager locationServicesEnabled] &&
([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedAlways
|| [CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse)) {
return YES;
}
return NO;
}
在回調方法中
//處理位置坐標更新
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
}
3、發起反向地理編碼檢索
//處理位置坐標更新
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
// NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
//發起反向地理編碼檢索
_geoSearcher = [[BMKGeoCodeSearch alloc] init];
_geoSearcher.delegate = self;
CLLocationCoordinate2D pt = (CLLocationCoordinate2D){userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude};
BMKReverseGeoCodeOption *reverseGeoCodeSearchOption = [[
BMKReverseGeoCodeOption alloc] init];
reverseGeoCodeSearchOption.reverseGeoPoint = pt;
BOOL flag = [_geoSearcher reverseGeoCode:reverseGeoCodeSearchOption];
if(flag)
{
NSLog(@"反geo檢索發送成功");
}
else
{
NSLog(@"反geo檢索發送失敗");
}
}
接收反向地理編碼的回調
// 接收反向地理編碼結果
-(void) onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:
(BMKReverseGeoCodeResult *)result
errorCode:(BMKSearchErrorCode)error{
if (error == BMK_SEARCH_NO_ERROR) {
// 在此處理正常結果
NSLog(@"%@--%@",result.addressDetail.city, result.address);
if ([[[NSUserDefaults standardUserDefaults] valueForKey:@"UserCurrentAddress"] isEqualToString:result.address]) { // 用戶當前位置與沙河存儲位置相同
// 關閉定位服務LocationService
[self stopLocation];
} else {
[[NSUserDefaults standardUserDefaults] setValue:result.addressDetail.city forKey:@"UserCurrentCity"]; // 將用戶城市位置存儲,以便后面進行設置地址時的city
[[NSUserDefaults standardUserDefaults] setValue:result.address forKey:@"UserCurrentAddress"];
}
}
else {
[self startLocation];
NSLog(@"抱歉,未找到結果");
}
}
具體代碼實現可以github源碼上面下載