百度地圖(集成大頭針和導(dǎo)航功能)

1.在百度地圖開(kāi)發(fā)者平臺(tái)申請(qǐng)AppKey.

  • 1.1 進(jìn)入百度地圖開(kāi)發(fā)者平臺(tái),使用自己的百度賬號(hào)來(lái)登錄就可以了,百度地圖除了個(gè)別功能都是免費(fèi)使用的.
  • 1.2 申請(qǐng)創(chuàng)建應(yīng)用,獲取AppKey.


    Snip20160525_15.png
  • 1.3注冊(cè)之后,會(huì)顯示這樣,如下圖:


    Snip20160525_16.png

2.配置開(kāi)發(fā)環(huán)境

  • 2.1百度地圖SDK提供兩種方式來(lái)配置,一種是通過(guò)CocoaPods來(lái)安裝(簡(jiǎn)單方便,一行命令行就可以安裝,前提是你要有CocoPods).另外一種是手動(dòng)配置.framework的開(kāi)發(fā)包(雖然相對(duì)于第一種來(lái)說(shuō)比較復(fù)雜,且有很多坑來(lái)等著你來(lái)跳.但后面我們需要后續(xù)開(kāi)發(fā)導(dǎo)航的SDK,需要用到基礎(chǔ)地圖的SDK).之前我是用CocoPods的方式來(lái)使用百度基礎(chǔ)地圖,但做到導(dǎo)航的時(shí)候,報(bào)了一堆百度基礎(chǔ)地圖庫(kù)文件沒(méi)關(guān)聯(lián)到ViewControl.O的錯(cuò)誤.所以我又重新手動(dòng)導(dǎo)入了一遍.所以你只想用基礎(chǔ)地圖功能的話,那么可以選擇第一種方式來(lái)安裝.當(dāng)然你需要后續(xù)開(kāi)發(fā)導(dǎo)航功能的話,那么建議用第二種手動(dòng)配置一下.(個(gè)人感覺(jué)百度地圖沒(méi)有做好CocoPod里面導(dǎo)航功能的關(guān)聯(lián),所以會(huì)導(dǎo)致這樣的錯(cuò)誤.)
  • 2.2 使用第一種CocoPod方式來(lái)安裝使用基礎(chǔ)地圖功能.(官方文檔已經(jīng)說(shuō)得很清楚,可以去看一下.我是喜歡用命令行來(lái)進(jìn)行安裝,簡(jiǎn)單方便).
    1.創(chuàng)建Podfile:
touch Podfile

2.編輯Podfile內(nèi)容如下:

pod 'BaiduMapKit' #百度地圖SDK

3.在Podfile所在的文件夾下輸入命令:

pod install (這個(gè)可能比較慢,請(qǐng)耐心等待……)

成功以后,會(huì)出現(xiàn)如下記錄:

Analyzing dependencies   Downloading dependencies   Installing BaiduMapKit (2.9.1)   Generating Pods project   Integrating client project   [!] Please close any current Xcode sessions and **use** `***.xcworkspace` **for** **this** project from now on. Sending stats

恭喜你已成功導(dǎo)入百度地圖IOS SDK,現(xiàn)在可以打開(kāi)xcodeSpace文件,在你的項(xiàng)目中使用百度地圖SDK了.

  • 2.3手動(dòng)配置.frameWork開(kāi)發(fā)包
    1.可以到官網(wǎng)看示例代碼
    Snip20160525_18.png

    2.下載完畢之后,打開(kāi)Xcode,在 TARGETS->Build Phases-> Link Binary With Libaries中點(diǎn)擊“+”按鈕,在彈出的窗口中點(diǎn)擊“Add Other”按鈕,選擇BaiduMapAPI_**.framework添加到工程中。(或者是直接把下載好的百度地圖API拖進(jìn)到工程里面,frameWord會(huì)自動(dòng)顯示,當(dāng)然是拖入到工程文件的路徑里面,這里要注意的是有一個(gè)存儲(chǔ)著圖片資源按的Bundle文件,如果你不導(dǎo)入的話,寄出地圖會(huì)沒(méi)有顯示,大頭針的系統(tǒng)視圖也沒(méi)有.這可以通過(guò)點(diǎn)擊protect->add to…添加就可以了).
Snip20160525_19.png
Snip20160525_20.png
Snip20160526_4.png
Snip20160526_6.png

3.導(dǎo)入所需要的系統(tǒng)庫(kù), 因此您需要在您的Xcode工程中引入CoreLocation.framework和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)。

Snip20160525_22.png

4.在TARGETS->Build Settings->Other Linker Flags 中添加-ObjC。

Snip20160525_24.png

5.最后,我們Command + B 一下,如果你能編譯成功了,那么就OK了.其實(shí)沒(méi)多少?gòu)?fù)雜的,基礎(chǔ)地圖是最簡(jiǎn)單的.(最后在自己使用的類里面如果使用百度地圖的類就導(dǎo)入一下就可以了).
6.IOS9之后改用Https形式,所以我們也要在Info.plist文件里配置一下,否則會(huì)影響百度地圖的使用(空白一片),還有Bundle Identifier要與申請(qǐng)APPKey的Bundle Identifier 一致.而且我們也在info添加它的displayName與申請(qǐng)時(shí)應(yīng)用名稱也要一致.

3.進(jìn)入開(kāi)發(fā),初始化BMKMapManager,(我們要拿到上面申請(qǐng)得到的APPKey)

Snip20160525_29.png

代碼如下:
在AppDelegate文件中,導(dǎo)入#import <BaiduMapAPI_Base/BMKMapManager.h>,且強(qiáng)引用一個(gè)mapManager

#import <BaiduMapAPI_Base/BMKMapManager.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) BMKMapManager *mapManager;
@end

在AppDelegate.m文件中,在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions中添加APPKey.

_mapManager = [[BMKMapManager alloc]init];
    BOOL ret = [_mapManager start:@"vsydf257xSgtVvn5IwbnoUKNhn7MRWeG" generalDelegate:nil];
    if (!ret) {
        NSLog(@"引擎啟動(dòng)失敗");
    }
    [BNCoreServices_Instance initServices:@"vsydf257xSgtVvn5IwbnoUKNhn7MRWeG"];
    [BNCoreServices_Instance startServicesAsyn:nil fail:nil];

在ViewDidLoad方法里面初始化MapView.上面忘記說(shuō)一件事,由于百度地圖引用的是靜態(tài)庫(kù),所以我們需要把其中一個(gè).m文件改成.mm文件.這樣才可以正常使用.

- (void)viewDidLoad {
    [super viewDidLoad];
    BMKMapView *mapView = [[BMKMapView alloc]initWithFrame:self.view.bounds];
    [self.view addSubview:mapView];
    self.mapView = mapView;
}

這樣基礎(chǔ)地圖信息就可以顯示出來(lái).當(dāng)然坐標(biāo)是在北京的,后面會(huì)教你怎么修改地圖顯示的坐標(biāo)和大頭針視圖.

Snip20160525_26.png

4.設(shè)置大頭針視圖和設(shè)置地圖顯示相對(duì)應(yīng)的位置.

我們知道地圖的定位是根據(jù)經(jīng)緯度來(lái)進(jìn)行判斷的.所以這里輸入我們想顯示地址的經(jīng)緯度,同樣我們要?jiǎng)?chuàng)建大頭針視圖,百度提供了2個(gè)大頭針類,一種是百度大頭針標(biāo)準(zhǔn)類,另一種是自定義類.這些都會(huì)用到BMKPointAnnotation類方法,而我們自定義大頭針可以遵守BMKMapViewDelegate方法,重寫它的協(xié)議方法.

- (void) viewDidAppear:(BOOL)animated {
    // 添加一個(gè)PointAnnotation
    BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];
    CLLocationCoordinate2D coor;
    coor.latitude = 22.50;
    coor.longitude = 113.41;
    annotation.coordinate = coor;
    [_mapView addAnnotation:annotation];
    //設(shè)置地圖的縮放比例
    [_mapView setZoomLevel:16];
    //地圖顯示的位置
    [self.mapView setCenterCoordinate:CLLocationCoordinate2DMake(22.50,113.41)];
}

- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{
    BMKPinAnnotationView *newAnationView = [[BMKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"anonationID"];
    //直接顯示,不用點(diǎn)擊彈出
    [newAnationView setSelected:YES];
    ((BMKPinAnnotationView *)newAnationView).image = [UIImage imageNamed:@"poi_3"];
    ((BMKPinAnnotationView *)newAnationView).animatesDrop = YES;
    UIView *popView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 185, 56)];
    //設(shè)置彈出氣泡背景圖片
    UIImageView *bgImageV =[[UIImageView alloc]init];
    bgImageV.image = [[UIImage imageNamed:@"wl_map_icon_5"]stretchableImageWithLeftCapWidth:28 topCapHeight:16];
    bgImageV.frame = CGRectMake(0, 0, 185, 52);
    [popView addSubview:bgImageV];
    UIImageView *connerImageV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"wl_map_icon_4"]];
    connerImageV.frame = CGRectMake(88, 52, 12, 4);
    [popView addSubview:connerImageV];
    UIImageView *navImageV = [[UIImageView alloc]initWithFrame:CGRectMake(120, 0, 65, 52)];
    navImageV.image = [UIImage imageNamed:@"wl_map_icon_1"];
    navImageV.userInteractionEnabled = YES;
    [bgImageV addSubview:navImageV];
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.text = @"廣東省中山市";
    titleLabel.textAlignment = NSTextAlignmentLeft;
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.frame = CGRectMake(0, 0, 120, 30);
    [bgImageV addSubview:titleLabel];
    UILabel *subLabel = [[UILabel alloc]init];
    subLabel.text = @"博愛(ài)五路今科科技公司";
    subLabel.textAlignment = NSTextAlignmentLeft;
    subLabel.textColor = [UIColor whiteColor];
    subLabel.font = [UIFont systemFontOfSize:12];
    subLabel.frame = CGRectMake(0, 30, 120, 22);
    [bgImageV addSubview:subLabel];
    BMKActionPaopaoView *pView = [[BMKActionPaopaoView alloc]initWithCustomView:popView];
    pView.frame = CGRectMake(0, 0, 185, 56);
    ((BMKPinAnnotationView*)newAnationView).paopaoView = nil;
    ((BMKPinAnnotationView*)newAnationView).paopaoView = pView;
    return newAnationView;
}

運(yùn)行程序,會(huì)顯示如下圖:

Snip20160525_27.png

5.下面的是百度地圖的導(dǎo)航功能開(kāi)發(fā)

  • 5.1 同樣地我們下載百度地圖導(dǎo)航SDK
  • 5.2 同樣我們?cè)贏PI創(chuàng)建應(yīng)用的時(shí)候只要選中了導(dǎo)航功能集成服務(wù),那么我們還是可以繼續(xù)用它的APPKey.下面我們把.framework添加到工程里面.
    把baiduNaviSDK文件夾添加到工程中,把AudioToolbox.framework、ImageIO.framework、CoreMotion.framework、CoreLocation.framework、CoreTelephony.framework、MediaPlayer.framework、AVFoundation.framework、SystemConfiguration.framework、libstdc++6.0.9.dylib這幾個(gè)framework添加到工程中,添加方法為在Xcode中單擊工程文件,選擇Build Phrases選項(xiàng),點(diǎn)擊Link Binary with Libraries下的“+”逐個(gè)添加,如下所示:
Snip20160525_30.png
  • 5.3如果工程是ARC的話,那么就在BuildSettings里設(shè)置ARC編譯為No.
Snip20160525_31.png
  • 5.4 Xcode7.0是默認(rèn)開(kāi)啟Enable Bitcode的,而這里我們需要把它關(guān)閉,否則會(huì)報(bào)這樣的錯(cuò)誤:
ld: 'XXX' does not contain bitcode. You must rebuild it with bitcode enabled  (Xcode setting ENABLE_BITCODE), obtain an updated library from the vendor, or disable bitcode for this target. for architecture arm64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
Snip20160525_32.png
  • 5.5其實(shí)還有一個(gè)坑等著你來(lái)跳,當(dāng)我們編譯的時(shí)候,無(wú)緣無(wú)故地報(bào)了一個(gè)錯(cuò),一看,原來(lái)是@synthesize of weak property is only allowed in arc or gc mode,由于現(xiàn)在大多數(shù)第三方庫(kù)都是用ARC來(lái)管理內(nèi)存的,但我們無(wú)法用weak在ARC環(huán)境下修飾.所以有2種解決方法,一種是將weak修飾的全部改為assign(這種做法太麻煩,不推薦).第二種比較簡(jiǎn)單,修改項(xiàng)目的配置文件.禁止某些文件運(yùn)行ARC機(jī)制,簡(jiǎn)單地來(lái)說(shuō)就是百度的第三方庫(kù)就用ARC來(lái)管理文件.自己創(chuàng)建的文件當(dāng)然是用MRC來(lái)管理.解決方法如下:


    Snip20160525_33.png
Snip20160526_2.png
  • 5.6 同樣我們也要在AppDelegate里面設(shè)置APPKey初始化導(dǎo)航引擎.因?yàn)榛A(chǔ)地圖和導(dǎo)航功能是分開(kāi)的,所以這里APPKey用到了2次.
#import "BNCoreServices.h"
[BNCoreServices_Instance initServices:@"vsydf257xSgtVvn5IwbnoUKNhn7MRWeG"];
    [BNCoreServices_Instance startServicesAsyn:nil fail:nil];
  • 5.7由于我們要設(shè)置路徑規(guī)劃,都是需要起點(diǎn)和終點(diǎn)的,所以這里我們?cè)O(shè)置起點(diǎn)為我們當(dāng)前的位置,而終點(diǎn)的話是傳入進(jìn)來(lái)的坐標(biāo)經(jīng)緯度.而我們這里就需要定位功能,開(kāi)啟定位服務(wù)來(lái)獲得坐標(biāo)經(jīng)緯度,再設(shè)置它為起點(diǎn).(百度的定位非常簡(jiǎn)單,優(yōu)化了很多,只是封裝了原生的定位服務(wù).我們只需調(diào)用它的代理方法就可以獲得它的最新坐標(biāo))
self.service = [[BMKLocationService alloc]init];
    self.service.delegate = self;
    [self.service startUserLocationService];
//開(kāi)啟定位功能,獲得坐標(biāo)方法
-(void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    //獲得坐標(biāo)經(jīng)度
    self.mylongitude = userLocation.location.coordinate.longitude;
    //獲得坐標(biāo)緯度
    self.mylatitude = userLocation.location.coordinate.latitude;
}
  • 5.8返回我們所需要設(shè)置導(dǎo)航功能的控制器里面(這里的是ViewControl.mm),我們這里是點(diǎn)擊大頭針上面的泡泡會(huì)實(shí)現(xiàn)導(dǎo)航功能
//點(diǎn)擊泡泡的代理方法(實(shí)現(xiàn)導(dǎo)航功能)
-(void)mapView:(BMKMapView *)mapView annotationViewForBubble:(BMKAnnotationView *)view
{
    //節(jié)點(diǎn)數(shù)組
    NSMutableArray *nodesArray = [[NSMutableArray alloc]    initWithCapacity:2];
    
    //起點(diǎn)
    BNRoutePlanNode *startNode = [[BNRoutePlanNode alloc] init];
    startNode.pos = [[BNPosition alloc] init];
    startNode.pos.x = self.mylongitude;
startNode.pos.y = self.mylatitude;       
startNode.pos.eType = BNCoordinate_BaiduMapSDK;
    [nodesArray addObject:startNode];
    
    //終點(diǎn)
    BNRoutePlanNode *endNode = [[BNRoutePlanNode alloc] init];
    endNode.pos = [[BNPosition alloc] init];
    endNode.pos.x = 113.41;
    endNode.pos.y = 22.50;
    endNode.pos.eType = BNCoordinate_BaiduMapSDK;
    [nodesArray addObject:endNode];
    //發(fā)起路徑規(guī)劃
    [BNCoreServices_RoutePlan startNaviRoutePlan:BNRoutePlanMode_Recommend naviNodes:nodesArray time:nil delegete:self userInfo:nil];
}
-(void)routePlanDidFinished:(NSDictionary *)userInfo
{
    NSLog(@"算路成功");
    
    //路徑規(guī)劃成功,開(kāi)始導(dǎo)航
    [BNCoreServices_UI showNaviUI: BN_NaviTypeReal delegete:self isNeedLandscape:YES];
}

由于代碼今天毀了/(ㄒoㄒ)/~~,所以以后會(huì)更新上傳代碼地址.或者有需要的朋友可以聯(lián)系我(QQ:1090981897)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容