地圖集成當然是按照百度API官方步驟來,看細節
-
獲取秘鑰,注意:安全碼一定得是Bundle Identifier
秘鑰獲取.png 集成SDK,使用CocoaPods集成
集成SDK.png
注意,這是我遇到的坑,導入BaiduMapAPI_Base不出來,自己添加完整,每一個包這個component
- import <BaiduMapAPI_Base/BMKMapComponent.h>
- 要添加Bundle display name
然后按官方文檔進行
#import "AppDelegate.h"
#import <BaiduMapAPI_Base/BMKMapManager.h>
@interface AppDelegate ()
/** 地圖管理者 */
@property (nonatomic, strong) BMKMapManager *mapManager;
@end
@implementation AppDelegate
- (BMKMapManager *)mapManager {
if (_mapManager == nil) {
_mapManager = [[BMKMapManager alloc] init];
BOOL ret = [_mapManager start:@"qcNQtv1Uz5WTsr9i59up62q9BBDh6T2W" generalDelegate:nil];
if (!ret) {
NSLog(@"manager start failed!");
} else {
NSLog(@"成功");
}
}
return _mapManager;
}
- 成功
百度地圖.png
- 檢索功能
周邊.png
- 有個坑就是,下面代碼應該放地圖視圖外,點擊白色區域
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//發起檢索
BMKNearbySearchOption *option = [[BMKNearbySearchOption alloc]init];
option.pageIndex = 0;
option.pageCapacity = 10;
option.location = (CLLocationCoordinate2D){39.915, 116.404};
option.keyword = @"小吃";
BOOL flag = [self.search poiSearchNearBy:option];
if(flag)
{
NSLog(@"周邊檢索發送成功");
}
else
{
NSLog(@"周邊檢索發送失敗");
}
}
搜索結果
#pragma mark - BMKPoiSearchDelegate
- (void)onGetPoiResult:(BMKPoiSearch*)searcher result:(BMKPoiResult*)poiResultList errorCode:(BMKSearchErrorCode)error
{
if (error == BMK_SEARCH_NO_ERROR) {
//在此處理正常結果
[poiResultList.poiInfoList enumerateObjectsUsingBlock:^(BMKPoiInfo * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"%@---%@", obj.name, obj.address);
}];
}
else if (error == BMK_SEARCH_AMBIGUOUS_KEYWORD){
//當在設置城市未找到結果,但在其他城市找到結果時,回調建議檢索城市列表
// result.cityList;
NSLog(@"起始點有歧義");
} else {
NSLog(@"抱歉,未找到結果");
}
}
- 導航集成
導航集成.png