新建工程后,几项准备:
1、工程中一个文件设为.mm后缀
2、在Xcode的Project -> Edit Active Target -> Build -> Linking -> Other Linker Flags中添加-ObjC
3、 设置静态库的链接路径,在Xcode的Project -> Edit Active Target -> Build -> Search Path -> Library Search Paths中添加您的静态库目录,比如"$(SRCROOT)/../libs/Release$(EFFECTIVE_PLATFORM_NAME)",$(SRCROOT)宏代表您的工程文件目录,$(EFFECTIVE_PLATFORM_NAME)宏代表当前配置是OS还是simulator
4、framework: CoreLocation.framework和QuartzCore.framework、OpenGLES.framework、SystemConfiguration.framework、CoreGraphics.framework、Security.framework
5、自iOS SDK v2.5.0起,为了对iOS8的定位能力做兼容,做了相应的修改,开发者在使用过程中注意事项如下: 需要在info.plist里添加(以下二选一,两个都添加默认使用NSLocationWhenInUseUsageDescription):
NSLocationWhenInUseUsageDescription ,允许在前台使用时获取GPS的描述
NSLocationAlwaysUsageDescription ,允许永久使用GPS的描述
首先要在appdelegate里面添加
BMKMapManager
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // 要使用百度地图,请先启动BaiduMapManager _mapManager = [[BMKMapManager alloc]init]; // 如果要关注网络及授权验证事件,请设定 generalDelegate参数 BOOL ret = [_mapManager start:@"在此处输入您的授权Key" generalDelegate:nil]; if (!ret) { NSLog(@"manager start failed!"); } // Add the navigation controller‘s view to the window and display. [self.window addSubview:navigationController.view]; [self.window makeKeyAndVisible]; return YES; }
定位:
@interface ViewController ()<BMKMapViewDelegate,BMKCloudSearchDelegate,BMKLocationServiceDelegate> { BMKMapView* mapView; BMKCloudSearch* search; BMKLocationService* location; }
- (void)viewDidLoad { [super viewDidLoad]; CGRect rect = CGRectMake(0, 0, 320, 400); mapView = [[BMKMapView alloc] initWithFrame:rect]; [self.view addSubview:mapView]; mapView.showsUserLocation= YES; //搜索 search = [[BMKCloudSearch alloc] init]; search.delegate = self; //服务 location = [[BMKLocationService alloc] init]; location.delegate = self; [location startUserLocationService]; mapView.showsUserLocation = NO; mapView.userTrackingMode = BMKUserTrackingModeFollowWithHeading; mapView.showsUserLocation = YES; //搜索按钮 UIButton* btSearch = [UIButton buttonWithType:UIButtonTypeCustom]; [btSearch setTitle:@"搜索" forState:UIControlStateNormal]; [btSearch addTarget:self action:@selector(clickSearch:) forControlEvents:UIControlEventTouchUpInside]; [btSearch setFrame:CGRectMake(0, 400, 320, 80)]; [btSearch setTintColor:[UIColor whiteColor]]; [btSearch setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [self.view addSubview:btSearch]; } -(void)viewWillAppear:(BOOL)animated{ [mapView viewWillAppear]; mapView.delegate = self; } -(void)viewWillDisappear:(BOOL)animated{ [mapView viewWillDisappear]; mapView.delegate = nil; }
mapview代理实现:
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation { [mapView updateLocationData:userLocation]; } //处理位置坐标更新 - (void)didUpdateUserLocation:(BMKUserLocation *)userLocation { [mapView updateLocationData:userLocation]; }
先记录这些,不用每次都去查百度蛋疼文档了。