廢話不多說直接開始:
準備工作: 申請百度賬號, 創建一個xcode項目
第一步: 獲取百度地圖密鑰
- 百度搜索 "百度地圖API", 進入百度地圖首頁
691669D6-04C2-4340-9B17-76E036F48BBD.png
550E5280-370D-43A5-9EAE-B7D946955B5F.png
3249C747-E32B-4842-9659-CB2972185EBC.png
8DC00895-DE00-4345-AAC1-98C7C0A8CDAC.png
注意點: 一定要開開發文檔的注意事項
第二步: 下載百度地圖SDK并配置環境(這里我選擇的是全部下載, 因為里面有事例代碼- -!)
將百度地圖SDK中所有的.framework拖進項目中
B34D3A2F-BACC-4180-9349-0FA717BA4E6E.png
AF884066-BB55-4DB8-8D6D-6D7D391814E4.png
D7BB245C-B4F3-451C-80A3-E53AA69165A2.png
常見問題:
- 根據開發文檔進行配置, 發現報錯(Undefined symbols for architecture x86_64:
),編譯不過去, 查看事例程序發現
在Xcode的Project -> Active Target ->Build Phases ->Link Binary With Libraries中添加 UIKit.framework 和 Foundation.framework 就通過編譯了
- 如果在工程中添加了百度地圖,報錯: 引擎初始化失敗, 地圖顯示空白,有可能是沒有導入百度地圖的資源文件( 選中工程名,在右鍵菜單中選擇Add Files to “工程名”…,從BaiduMapAPI_Map.framework||Resources文件中選擇mapapi.bundle文件,并勾選“Copy items if needed”復選框,單擊“Add”按鈕,將資源文件添加到工程中。)
- POI 檢索的時候,如果報錯: 抱歉,未找到結果, 打印error,如果是:( BMK_SEARCH_PERMISSION_UNFINISHED,///還未完成鑒權,請在鑒權通過后重試
) , 解決辦法: 不要在ViewDidLoad等初始化方法中直接加載POI檢索的內容, 加載POI檢索,我們要把他放到點擊, 查找等事件中完成, 才不會報錯
這就完成了百度地圖的SDK,做POI搜索,代碼如下:
#import "AppDelegate.h"
#import <BaiduMapAPI_Map/BMKMapView.h>//只引入所需的單個頭文件
#define kBMK_Key @"EeTjyB8CMlvMGQt7ScqADTolEgtNsfAQ"
@interface AppDelegate ()
@property (nonatomic, strong) BMKMapManager *mapManager;
@end
@implementation AppDelegate
- (BMKMapManager *)mapManager
{
if (_mapManager == nil) {
_mapManager = [[BMKMapManager alloc]init];
BOOL ret = [_mapManager start:kBMK_Key generalDelegate:nil];
if (!ret) {
NSLog(@"BMKManager start failed!");
} else{
NSLog(@"BMKManager start success!");
}
}
return _mapManager;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.mapManager;
return YES;
}
#import "ViewController.h"
#import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相關所有的頭文件
#import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地圖功能所有的頭文件
#import <BaiduMapAPI_Search/BMKSearchComponent.h>//引入檢索功能所有的頭文件
#import <BaiduMapAPI_Cloud/BMKCloudSearchComponent.h>//引入云檢索功能所有的頭文件
#import <BaiduMapAPI_Location/BMKLocationComponent.h>//引入定位功能所有的頭文件
#import <BaiduMapAPI_Utils/BMKUtilsComponent.h>//引入計算工具所有的頭文件
#import <BaiduMapAPI_Radar/BMKRadarComponent.h>//引入周邊雷達功能所有的頭文件
#import <BaiduMapAPI_Map/BMKMapView.h>//只引入所需的單個頭文件
@interface ViewController () <BMKMapViewDelegate, BMKPoiSearchDelegate>
@property (weak, nonatomic) IBOutlet BMKMapView *mapView;
@property (nonatomic,strong) BMKPoiSearch *poiSearch;
@end
@implementation ViewController
- (BMKPoiSearch *)poiSearch
{
if (_poiSearch == nil) {
_poiSearch = [[BMKPoiSearch alloc] init];
_poiSearch.delegate = self;
}
return _poiSearch;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.mapView.delegate = self;
}
// 地圖長按的返回方法 (獲取長按的經緯度,查找這里的小吃)
- (void)mapview:(BMKMapView *)mapView onLongClick:(CLLocationCoordinate2D)coordinate
{
BMKNearbySearchOption *option = [[BMKNearbySearchOption alloc]init];
option.pageIndex = 0;
option.pageCapacity = 10;
option.location = coordinate;
option.keyword = @"小吃";
BOOL flag = [self.poiSearch poiSearchNearBy:option];
if(flag)
{
NSLog(@"周邊檢索發送成功");
}
else
{
NSLog(@"周邊檢索發送失敗");
}
// 將顯示區域放大到適當的大小
// [self.mapView setRegion:<#(BMKCoordinateRegion)#>];
// 中心
CLLocationCoordinate2D center = option.location;
// 范圍 (這個范圍,我們可以在百度地圖區域改變的代理方法中獲得區域改變的范圍, 我們調整放大縮小地圖,找到最合適的區域范圍)
BMKCoordinateSpan span = BMKCoordinateSpanMake(0.017092, 0.015622);
BMKCoordinateRegion region = BMKCoordinateRegionMake(center, span);
[self.mapView setRegion:region animated:YES];
}
// 范圍 (這個范圍,我們可以在百度地圖區域改變的代理方法中獲得區域改變的范圍, 我們調整放大縮小地圖,找到最合適的區域范圍)
- (void)mapView:(BMKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
NSLog(@"%f---%f",mapView.region.span.latitudeDelta, mapView.region.span.longitudeDelta);
}
// 根據經緯度添加大頭針
- (void)addAnnotationWihtPT:(CLLocationCoordinate2D)coor addTitle:(NSString *)title addAddress:(NSString *)address
{
BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];
annotation.coordinate = coor;
annotation.title = title;
annotation.subtitle = address;
[_mapView addAnnotation:annotation];
}
#pragma mark - BMKMapViewDelegate
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{
if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
// 大頭針的循環利用
static NSString *ID = @"Annotation";
BMKPinAnnotationView *newAnnotationView = (BMKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:ID];
if (newAnnotationView == nil) {
newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:ID];
}
// 如果在重用里面取出的話,不走上面if的話
// 這句話重新賦值不要忘記, 防止循環利用出錯
newAnnotationView.annotation = annotation;
newAnnotationView.pinColor = BMKPinAnnotationColorPurple;
newAnnotationView.animatesDrop = YES;// 設置該標注點動畫顯示
UIButton *btn = [UIButton buttonWithType:UIButtonTypeContactAdd];
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
newAnnotationView.rightCalloutAccessoryView = btn;
return newAnnotationView;
}
return nil;
}
- (void)btnClick:(UIButton *)button
{
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) {
[self addAnnotationWihtPT:obj.pt addTitle:obj.name addAddress:obj.address];
}];
}
else if (error == BMK_SEARCH_AMBIGUOUS_KEYWORD){
//當在設置城市未找到結果,但在其他城市找到結果時,回調建議檢索城市列表
// result.cityList;
NSLog(@"起始點有歧義");
} else {
NSLog(@"抱歉,未找到結果---%d", error);
}
}
//不使用時將delegate設置為 nil
-(void)viewWillDisappear:(BOOL)animated
{
_poiSearch.delegate = nil;
}
1.gif
下面開始做導航了
第四步: 下載百度導航SDK并配置環境
配置環境不過多贅述了, 開發文檔上寫的很全...
環境配置注意點, 文檔上沒有的:
- 將BuildSetting->Build Active Architecture Only ->NO
- 將BuildSetting->Valid Architectures->"armv7 arm64" 刪掉 armv7s
- 將BuildSetting->Enable Bitcode -> NO
- iOS導航SDK 語音播報 需要添加白名單, 否則會TTS授權失敗,
FD6B7B5E-1634-420D-90CC-9C7729E70E91.png
重要的一個問題 : 如果上面地圖的SDK導入用的是.mm方式的話,兩個SDK是兼容的, 如果用(工程屬性中指定編譯方式,即在Xcode的Project -> Edit Active Target -> Build Setting 中找到 Compile Sources As,并將其設置為"Objective-C++"的方式的話會有問題)
百度地圖報錯:Cannot initialize a variable of type 'const char *' with an rvalue of type 'const void *', 這時候,將工程屬性中指定編譯方式,即在Xcode的Project -> Edit Active Target -> Build Setting 中找到 Compile Sources As,并將其設置為默認
導航代碼如下:
// 在選擇大頭針的時候,記錄當前大頭針所在的經緯度
- (void)mapView:(BMKMapView *)mapView didSelectAnnotationView:(BMKAnnotationView *)view
{
_selectAnnotation = view.annotation;
}
- (void)btnClick:(UIButton *)button
{
CLLocationCoordinate2D coordinate = _selectAnnotation.coordinate;
[self startNaviToEndPT:coordinate];
}
#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) {
[self addAnnotationWihtPT:obj.pt addTitle:obj.name addAddress:obj.address];
}];
}
else if (error == BMK_SEARCH_AMBIGUOUS_KEYWORD){
//當在設置城市未找到結果,但在其他城市找到結果時,回調建議檢索城市列表
// result.cityList;
NSLog(@"起始點有歧義");
} else {
NSLog(@"抱歉,未找到結果---%d", error);
}
}
//發起導航
- (void)startNaviToEndPT:(CLLocationCoordinate2D)endPT
{
//節點數組
NSMutableArray *nodesArray = [[NSMutableArray alloc] initWithCapacity:2];
//起點
BNRoutePlanNode *startNode = [[BNRoutePlanNode alloc] init];
startNode.pos = [[BNPosition alloc] init];
startNode.pos.x = 113.936392;
startNode.pos.y = 22.547058;
startNode.pos.eType = BNCoordinate_BaiduMapSDK;
[nodesArray addObject:startNode];
//終點
BNRoutePlanNode *endNode = [[BNRoutePlanNode alloc] init];
endNode.pos = [[BNPosition alloc] init];
endNode.pos.x = endPT.longitude;
endNode.pos.y = endPT.latitude;
endNode.pos.eType = BNCoordinate_BaiduMapSDK;
[nodesArray addObject:endNode];
//發起路徑規劃
[BNCoreServices_RoutePlan startNaviRoutePlan:BNRoutePlanMode_Recommend naviNodes:nodesArray time:nil delegete:self userInfo:nil];
}
#pragma mark - BNNaviRoutePlanDelegate
//算路成功回調
-(void)routePlanDidFinished:(NSDictionary *)userInfo
{
NSLog(@"算路成功");
//路徑規劃成功,開始導航
/*
BN_NaviTypeReal, < 真實導航
BN_NaviTypeSimulator < 默認模擬導航
*/
[BNCoreServices_UI showNaviUI: BN_NaviTypeSimulator delegete:self isNeedLandscape:YES];
}
2.gif