百度地圖其實和高德地圖差不錯,方法名也很像。php
1、註冊開發者賬號,下載百度SDKnode
註冊開發者賬號很簡單ios
SDK下載地址:http://lbsyun.baidu.com/index.php?title=iossdk/sdkiosdev-downloadc++
2、配置環境sql
bundle identifierapi
得到AKxcode
2.1能夠手動添加framework,也能夠使用CocoaPods,我是手動。ide
添加測試
CoreLocation.frameworkui
QuartzCore.framework
OpenGLES.framework
SystemConfiguration.framework
CoreGraphics.framework
Security.framework
libsqlite3.0.tbd(xcode7之前爲 libsqlite3.0.dylib)
CoreTelephony.framework
libstdc++.6.0.9.tbd(xcode7之前爲libstdc++.6.0.9.dylib)
2.2在info.plist文件中添加
NSLocationAlwaysUsageDescription//不加有時定位會不成功
NSLocationWhenInUseUsageDescription//不加定位有時會不成功
App Transport Security Settings -- Allow Arbitrary Loads(YES);
Bundle display name 填寫AK(APPKey)//會提示添加Bundle display name;
2.3千萬記得在SDK里加上mapapi//會提示配置不完整,而且地圖沒法顯示
#import "ViewController.h" #import <BaiduMapAPI_Base/BMKBaseComponent.h> #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> #define kScreenWidth [UIScreen mainScreen].bounds.size.width #define kScreenHeight [UIScreen mainScreen].bounds.size.height @interface ViewController ()<BMKMapViewDelegate,BMKLocationServiceDelegate> { BMKMapManager *_mapManager; BMKMapView * _mapView; BMKLocationService * _loactionService; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self createMapManager]; [self createMap]; [self loaction]; } //初始化地圖 - (void)createMapManager { _mapManager = [[BMKMapManager alloc]init]; BOOL result = [_mapManager start:@"應用的APPKey" generalDelegate:nil]; if (!result) { NSLog(@"manager 初始化失敗"); }else { NSLog(@"manager 初始化成功"); } } //建立地圖 - (void)createMap { _mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)]; [self.view addSubview:_mapView]; _mapView.delegate = self; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } //定位 - (void)loaction { _loactionService = [[BMKLocationService alloc]init]; _loactionService.delegate = self; } //處理方向改變信息 -(void)didUpdateUserHeading:(BMKUserLocation *)userLocation { } //處理座標位置更新 -(void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation { NSLog(@"%f",userLocation.location.coordinate.latitude); } //衛星地圖模式 - (IBAction)satelliteButton:(id)sender { [_mapView setMapType:BMKMapTypeSatellite]; } @end
百度導航
添加
AudioToolbox.framework
ImageIO.framework
CoreMotion.framework
CoreLocation.framework
CoreTelephony.framework
MediaPlayer.framework
AVFoundation.framework
SystemConfiguration.framework
libstdc++6.0.9.dylib
Security.framework
JavaScriptCore.framework
build
配置
#import "AppDelegate.h" #import "BNCoreServices.h" - (void)createPlito { NSString * baiduKey = [[LXJFrame readPlist:@"Confit" Type:@"plist"]objectForKey:@"baiduAppKey"][0]; [BNCoreServices_Instance initServices:baiduKey]; [BNCoreServices_Instance startServicesAsyn:nil fail:nil]; }
#import "PliotViewController.h" #import "BaiduPliotHeader.h" @interface PliotViewController ()<BNNaviRoutePlanDelegate,BNNaviUIManagerDelegate> { } //導航類型 分爲真實導航和模擬導航 @property (assign, nonatomic) BN_NaviType naviType; @end @implementation PliotViewController - (void)viewDidLoad { [super viewDidLoad]; [self createPageSet]; [self createPageSet]; [self getCoordinate]; } #pragma mark - create - (void)createPliotSet { _naviType = BN_NaviTypeReal; } - (void)createPageSet { self.title = @"導航"; self.view.backgroundColor = [UIColor whiteColor]; } - (BOOL)checkServicesInited { if(![BNCoreServices_Instance isServicesInited]) { UIAlertController * alerCT = [UIAlertController alertControllerWithTitle:@"提示" message:@"引擎還沒有初始化完成,請稍後再試" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction * okAction = [UIAlertAction actionWithTitle:@"我知道了" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { }]; [alerCT addAction:okAction]; return NO; } return YES; } - (void)getCoordinate { NSMutableArray * nodesArray = [[NSMutableArray alloc]init]; BNRoutePlanNode *startNode = [[BNRoutePlanNode alloc] init]; startNode.pos = [[BNPosition alloc] init]; startNode.pos.x = 113.948222; startNode.pos.y = 22.549555; startNode.pos.eType = BNCoordinate_BaiduMapSDK; [nodesArray addObject:startNode]; BNRoutePlanNode *endNode = [[BNRoutePlanNode alloc] init]; endNode.pos = [[BNPosition alloc] init]; endNode.pos.x = 114.089863; endNode.pos.y = 22.546236; endNode.pos.eType = BNCoordinate_BaiduMapSDK; [nodesArray addObject:endNode]; [BNCoreServices_RoutePlan startNaviRoutePlan:BNRoutePlanMode_Highway naviNodes:nodesArray time:nil delegete:self userInfo:nil]; } #pragma mark - BNNaviRoutePlanDelegate //算路成功回調 -(void)routePlanDidFinished:(NSDictionary *)userInfo { [LXJFrame showMessage:@"路徑規劃成功" showTime:1.5]; //路徑規劃成功,開始導航 [BNCoreServices_UI showNaviUI:_naviType delegete:self isNeedLandscape:YES]; } //算路失敗回調 - (void)routePlanDidFailedWithError:(NSError *)error andUserInfo:(NSDictionary *)userInfo { [LXJFrame showMessage:@"路徑規劃失敗" showTime:1.5]; if ([error code] == BNRoutePlanError_LocationFailed) { NSLog(@"獲取地理位置失敗"); } else if ([error code] == BNRoutePlanError_LocationServiceClosed) { NSLog(@"定位服務未開啓"); } } //算路取消回調 -(void)routePlanDidUserCanceled:(NSDictionary*)userInfo { [LXJFrame showMessage:@"路徑規劃取消" showTime:1.5]; } #pragma mark - BNNaviUIManagerDelegate //退出導航回調 -(void)onExitNaviUI:(NSDictionary*)extraInfo { [LXJFrame showMessage:@"退出導航" showTime:1.5]; }
要用真機測試