申請秘鑰
- 百度地圖iOS SDK開發密鑰的申請地址為:http://lbsyun.baidu.com/apiconsole/key
-
第一步:打開API控制臺,如下圖所示:
key2.png -
第二步:點擊創建應用,開始申請開發密鑰,如下圖:
key3.png -
第三步:填寫應用名稱、應用類型注意選擇“iOS SDK”、正確填寫安全碼,點擊確認,系統將會自動幫您生成相應的開發密鑰:
key4.png -
第四步:控制臺列表中的“訪問應用(ak)”就是您在開發過程中需要用到的開發密鑰,請妥善保管。
sdkios4.jpg - 由于iOS9改用更安全的https,為了能夠在iOS9中正常使用地圖SDK,請在"Info.plist"中進行如下配置,否則影響SDK的使用。
Unknown.png
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
- 如果在iOS9中使用了調起百度地圖客戶端功能,必須在"Info.plist"中進行如下配置,否則不能調起百度地圖客戶端。
<key>LSApplicationQueriesSchemes</key>
<array>
<string>baidumap</string>
</array>
- 安裝CocoaPods 使用CocoaPods導入地圖SDK
- 第一步,修改您的ViewController.h文件,添加以下代碼,使您的ViewController實現BMKMapViewDelegate協議:
#import <UIKit/UIKit.h>
#import "BMapKit.h"
@interface AnnotationDemoViewController : UIViewController <bmkmapviewdelegate> {
IBOutlet BMKMapView* _mapView;
}
@end```
* 修改您的.m文件,實現BMKMapViewDelegate的_mapView:viewForAnnotation:函數,并在viewDidAppear添加標注數據對象,核心代碼如下:
- (void) viewDidAppear:(BOOL)animated {
// 添加一個PointAnnotation
BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];
CLLocationCoordinate2D coor;
coor.latitude = 39.915;
coor.longitude = 116.404;
annotation.coordinate = coor;
annotation.title = @"這里是北京";
[_mapView addAnnotation:annotation];
} - (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{
if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];
newAnnotationView.pinColor = BMKPinAnnotationColorPurple;
newAnnotationView.animatesDrop = YES;// 設置該標注點動畫顯示
return newAnnotationView;
}
return nil;
}
* 如果要關注網絡及授權驗證事件,請設定 generalDelegate參數
-(void)map{
_mapManager = [[BMKMapManager alloc]init];
// 如果要關注網絡及授權驗證事件,請設定 generalDelegate參數
BOOL ret = [_mapManager start:@"sH4enIjfG585foGEtlIghsog" generalDelegate:nil];
if (!ret) {
NSLog(@"manager start failed!");
}
}
* 具體請參考文檔
http://lbsyun.baidu.com/index.php?title=iossdk/guide/basicmap