百度官方流程:http://developer.baidu.com/map/sdkiosdev-1.htm#.E7.AE.80.E4.BB.8B3
http://developer.baidu.com/map/sdkiosdev-2.htm
今天跟着百度LBS的官方走了一遍地图初始的应用设置。
首先必然是从百度下载官方的sdk,在官方申请新的key,如图:
类型,因为是移动应用,所以我选择的是For Mobile,引入静态库、frame神马的,直接走就行,比较重要的,一个是otherLinkerFlags必须加-ObjC,另一个就是项目里必须有个.mm文件,随便弄一个没用的就ok。
接下来就贴代码了:
AppDelegate:
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate,BMKMapViewDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) BMKMapManager *mapManager;
@end
.m文件:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
_mapManager = [[BMKMapManager alloc]init];
// 如果要关注网络及授权验证事件,请设定 generalDelegate参数
BOOL ret = [_mapManager start:@"baidu分给你的key" generalDelegate:nil];
if (!ret) {
NSLog(@"manager start failed!");
}
return YES;
}
viewController.h:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property(strong,nonatomic)BMKMapView* mapView;
@end
.m文件:
#import "ViewController.h"
@interface ViewController ()
@property(strong,nonatomic) id <BMKMapViewDelegate> delegate;
@end
@implementation ViewController
@synthesize mapView;
@synthesize delegate;
-(void)viewWillAppear:(BOOL)animated
{
[mapView viewWillAppear];
mapView.delegate = delegate;
}
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect frame=self.view.frame;
mapView = [[BMKMapView alloc]initWithFrame:frame];
self.view = mapView;
}
-(void)viewWillDisappear:(BOOL)animated
{
[mapView viewWillDisappear];
mapView.delegate = nil;
}