申請賬號
百度SDK官網(wǎng)
http://lbsyun.baidu.com/index.php?title=%E9%A6%96%E9%A1%B5
創(chuàng)建應(yīng)用
官方步驟
http://lbsyun.baidu.com/index.php?title=iossdk
1、申請key
2壤巷、創(chuàng)建新的應(yīng)用
3灾搏、填寫信息
4缴淋、獲得key
安裝pod
由于我是通過pod進(jìn)行集成百度地圖趾唱,所以要先安裝pod
如果是碰到ruby升級<=2.2.2的問題的朋友,可以裝完rvm之后執(zhí)行這條命令
$gem sources --remove https://rubygems.org/
運(yùn)行完這行命令腺晾,繼續(xù)
$ gem sources -a https://gems.ruby-china.org
$sudo gem install -n /usr/local/bin cocoapods --pre
把master內(nèi)容解壓到/Users/你的用戶名/.cocoapods/repos路徑下
master內(nèi)容是cocoapodsmaster.zip捆昏。日后云盤文件添加
創(chuàng)建工程
官方教程http://lbsyun.baidu.com/index.php?title=iossdk/guide/attention
不用故事版創(chuàng)建
1、正常創(chuàng)建工程
2淌友、修改plist內(nèi)容
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>baidumap</string>
</array>
NSLocationWhenInUseUsageDescription
NSLocationAlwaysUsageDescription
二選一添加
Bundle display name
添加內(nèi)容
3煌恢、將AppDelegate.m改為AppDelegate.mm
4、添加Podfile
platform :ios, "8.0"
pod 'BaiduMapKit'
target :'項目名' do
end
pod install
等待一會震庭,打開workspace文件瑰抵。就集成好了。
5器联、準(zhǔn)備讓地圖顯示到view
-
修改默認(rèn)的main
-
AppDelegate.h
#import <UIKit/UIKit.h>
#import <BaiduMapAPI_Base/BMKBaseComponent.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate, BMKGeneralDelegate>
{
UINavigationController *navigationController;
BMKMapManager* _mapManager;
}
@property (strong, nonatomic) UIWindow *window;
@end
-
AppDelegate.mm
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
_mapManager = [[BMKMapManager alloc]init];
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor orangeColor];
BOOL ret = [_mapManager start:@"自己的key" generalDelegate:nil];
if (!ret) {
NSLog(@"manager start failed!");
}
ViewController * vc = [[ViewController alloc] init];
self.window.rootViewController = vc;
[self.window makeKeyAndVisible];
return YES;
}
-
ViewController.m
#import "ViewController.h"
#import <BaiduMapAPI_Base/BMKBaseComponent.h>
#include <BaiduMapAPI_Map/BMKMapView.h>
@interface ViewController ()<BMKMapViewDelegate>
@property(nonatomic, strong)BMKMapView* mapView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
self.view = _mapView;
}
-(void)viewWillAppear:(BOOL)animated
{
[_mapView viewWillAppear];
_mapView.delegate = self; // 此處記得不用的時候需要置nil二汛,否則影響內(nèi)存的釋放
}
-(void)viewWillDisappear:(BOOL)animated
{
[_mapView viewWillDisappear];
_mapView.delegate = nil; // 不用時,置nil
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
運(yùn)行
用storyboard模式使用
之前步驟一樣拨拓,只不過不使用AppDelegate肴颊,里面不寫東西了。
直接在ViewController中寫內(nèi)容渣磷,同樣能顯示
#import "ViewController.h"
#import <BaiduMapAPI_Map/BMKMapView.h>
#import <BaiduMapAPI_Base/BMKBaseComponent.h>
@interface ViewController ()
@property(nonatomic, strong)BMKMapManager * mapManager;
@property(nonatomic, strong)BMKMapView * mapView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor orangeColor];
_mapManager = [[BMKMapManager alloc] init];
BOOL result = [_mapManager start:@"emMQtEQNOkLCXA4e6dbE5ZKshb94sGkN" generalDelegate:nil];
if (!result) {
NSLog(@"Failed");
}
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
_mapView = [[BMKMapView alloc] initWithFrame:CGRectMake(0, 20, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)-20)];
[self.view addSubview:_mapView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end