關于百度地圖SDK的使用網上太多了,本人也是在實際開發中碰到了這樣的需求所以仔細看了SDK開發文檔,又在網上借鑒了些資料。黃天不負有心人,終于弄出來了,今天在此記錄下。
準備工作:下載地圖SDK
一、申請密鑰
申請密鑰百度地圖開發者文檔寫的很詳細了,在這里我就不廢話了直接上鏈接 ?申請密鑰
二、配置開發環境
1、手動導入.framework形式開發包
BaiduMapAPI_Base.framework為基礎包,使用SDK任何功能都需導入,其他分包可按需導入。將所需的BaiduMapAPI_**.framework拷貝到工程所在文件夾下。在 TARGETS->Build Phases-> Link Binary With Libaries中點擊“+”按鈕,在彈出的窗口中點擊“Add Other”按鈕,選擇BaiduMapAPI_**.framework添加到工程中
這里我用到這幾個包:
需要注意: 靜態庫中采用Objective-C++實現,因此需要您保證您工程中至少有一個.mm后綴的源文件(您可以將任意一個.m后綴的文件改名為.mm),或者在工程屬性中指定編譯方式,即在Xcode的Project -> Edit Active Target -> Build Setting 中找到 Compile Sources As,并將其設置為"Objective-C++"
2、引入系統庫
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)。
3、配置環境
在TARGETS->Build Settings->Other Linker Flags 中添加-ObjC。
4、引入mapapi.bundle資源文件
如果使用了基礎地圖功能,需要添加該資源,否則地圖不能正常顯示mapapi.bundle中存儲了定位、默認大頭針標注View及路線關鍵點的資源圖片,還存儲了矢量地圖繪制必需的資源文件。如果不需要使用內置的圖片顯示功能,則可以根據具體需求任意替換或刪除該bundle中image文件夾的圖片文件。
方法:選中工程名,在右鍵菜單中選擇Add Files to “工程名”…,從BaiduMapAPI_Map.framework||Resources文件中選擇mapapi.bundle文件,并勾選“Copy items if needed”復選框,單擊“Add”按鈕,將資源文件添加到工程中。
5、更改info.plist
iOS9改用更安全的https,為了能夠在iOS9中正常使用地圖SDK,需要在"Info.plist"中進行如下配置,否則影響SDK的使用。
需要在info.plist里添加(以下二選一,兩個都添加默認使用NSLocationWhenInUseUsageDescription):
NSLocationWhenInUseUsageDescription ,允許在前臺使用時獲取GPS的描述
NSLocationAlwaysUsageDescription ,允許永久使用GPS的描述
在使用Xcode6進行SDK開發過程中,需要在info.plist中添加:Bundle display name ,且其值不能為空(Xcode6新建的項目沒有此配置,若沒有會造成manager start failed)
三、添加代碼
1、初始化BMKMapManager
在AppDelegate.h中添加
#import<BaiduMapAPI_Base/BMKBaseComponent.h>
#import<BaiduMapAPI_Base/BMKMapManager.h>
@interface AppDelegate : UIResponder
{
BMKMapManager* _mapManager;
}
@property (strong, nonatomic) UIWindow *window;
@end
在AppDelegate.m的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions中添加
_mapManager = [[BMKMapManager alloc]init];
BOOL ret = [_mapManager start:@"在此處輸入您的授權Key"? generalDelegate:nil];
if (!ret) {
NSLog(@"manager start failed!");
}
在需要用的的ViewController中使用
viewWillAppear、viewWillDisappear方法來控制BMKMapView的生命周期,并且在一個時刻只能有一個BMKMapView接受回調消息,因此在使用BMKMapView的viewController中需要在viewWillAppear、viewWillDisappear方法中調用BMKMapView的對應的方法,并處理delegate,代碼如下:
#import<BaiduMapAPI_Map/BMKMapView.h>
#import<BaiduMapAPI_Location/BMKLocationService.h>
#import<BaiduMapAPI_Search/BMKGeocodeSearch.h>
#import<BaiduMapAPI_Map/BMKMapComponent.h>
#import<BaiduMapAPI_Search/BMKPoiSearchType.h>
需要添加代理
BMKMapViewDelegate,BMKLocationServiceDelegate,BMKGeoCodeSearchDelegate
{
? ? ? ?BMKMapView *_mapView;? //地圖
? ? ? ?BMKLocationService *_locService;? //定位
? ? ? ?BMKGeoCodeSearch *_geocodesearch; //地理編碼主類,用來查詢、返回結果信息
}
-(void)viewWillAppear:(BOOL)animated
{
[_mapView viewWillAppear];
_mapView.delegate = self; // 此處記得不用的時候需要置nil,否則影響內存的釋放
}
-(void)viewWillDisappear:(BOOL)animated
{
[_mapView viewWillDisappear];
_mapView.delegate = nil; // 不用時,置nil
}
- (void)viewDidLoad {
[super viewDidLoad];
//添加地圖視圖
_mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
_mapView.showsUserLocation = YES; //是否顯示定位圖層(即我的位置的小圓點)
_mapView.zoomLevel = 19;//地圖顯示比例
_mapView.mapType = BMKMapTypeStandard;//設置地圖為空白類型
[self.view addSubview:_mapView];
//
_geocodesearch = [[BMKGeoCodeSearch alloc] init];
_geocodesearch.delegate = self;
[self startLocation];//開始定位方法
}
//開始定位
-(void)startLocation
{
//初始化BMKLocationService
_locService = [[BMKLocationService alloc]init];
_locService.delegate = self;
_locService.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
//啟動LocationService
[_locService startUserLocationService];
}
//實現相關delegate 處理位置信息更新
//處理方向變更信息
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation
{
NSLog(@"heading is %@",userLocation.heading);
}
//處理位置坐標更新
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
//普通態
//以下_mapView為BMKMapView對象
[_mapView updateLocationData:userLocation]; //更新地圖上的位置
_mapView.centerCoordinate = userLocation.location.coordinate; //更新當前位置到地圖中間
//地理反編碼
BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc]init];
reverseGeocodeSearchOption.reverseGeoPoint = userLocation.location.coordinate;
BOOL flag = [_geocodesearch reverseGeoCode:reverseGeocodeSearchOption];
if(flag){
NSLog(@"反geo檢索發送成功");
[_locService stopUserLocationService];
}else{
NSLog(@"反geo檢索發送失敗");
}
}
#pragma mark -------------地理反編碼的delegate---------------
-(void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
{
NSLog(@"address:%@----%@",result.addressDetail,result.address);
//addressDetail: ? ? 層次化地址信息
//address: ? ?地址名稱
//businessCircle: ?商圈名稱
// location: ?地址坐標
//? poiList: ? 地址周邊POI信息,成員類型為BMKPoiInfo
}
//定位失敗
- (void)didFailToLocateUserWithError:(NSError *)error{
NSLog(@"error:%@",error);
}
終于寫完了,其實弄過一次就沒有感覺太復雜了,希望可以幫到有需要的朋友。