百度地图使用
百度地图使⽤用1.注册应⽤用
⾸首先你需要注册到百度开发者平台(http://lbsyun.baidu.com/apiconsole/key),然后注册⼀一个应⽤用
百度地图使用
- 1初始化百度地图5.1 在AppDelegate中引⼊入头⽂文件
import
<BaiduMapAPI_Base/BMKBaseComponent.h>
让AppDeleGate遵循BMKGeneralDelegate@interface AppDelegate()<BMKGeneralDelegate>
@end@implementation AppDelegate
{ BMKMapManager* _mapManager;
}
8.2 初始化百度地图在- (BOOL)application:(UIApplication
*)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions中加⼊入以下代码:
_mapManager = [[BMKMapManageralloc]init];
// 如果要关注⺴⽹网络及授权验证事件,请设定generalDelegate参数
BOOL ret = [_mapManagerstart:@"IOxjbOFry3vXYtjvO4yaRZCQ"generalDelegate:self];
if (!ret) { NSLog(@"manager start failed!");
}
其中@"IOxjbOFry3vXYtjvO4yaRZCQ”为你在第⼀一步注册的应⽤用AK
8.3 实现代理
pragma mark------百度地图代理------- (void)onGetNetworkState:(int)iError{
if (0 == iError) {NSLog(@"联⺴⽹网成功");
}else{
NSLog(@"onGetNetworkState %d",iError); }
}-(void)onGetPermissionState:(int)iError{
if (0 == iError) {NSLog(@"授权成功");
}else {
NSLog(@"onGetPermissionState %d",iError
);
}}
8.4 管理百度地图的⽣生命周期
//应⽤用将进⼊入后台
-(void)applicationWillResignActive:(UIApplication *)application {
[BMKMapView willBackGround];}
//程序进⼊入前台
-(void)applicationDidBecomeActive:(UIApplication *)application {
[BMKMapView didForeGround];}
import "ViewController.h"
import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相关所有的头文件
import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地图功能所有的头文件
import <BaiduMapAPI_Location/BMKLocationComponent.h>//引入定位功能所有的头文件
@interface ViewController ()<BMKMapViewDelegate,BMKLocationServiceDelegate>
@end
@implementation ViewController
{
BMKMapView * _mapView;//百度地图
BMKLocationService* _locService;//定位服务
}
-
(void)viewDidLoad {
[super viewDidLoad];// 初始化百度地图
_mapView = [[BMKMapView alloc] init];
// self.view = _mapView;
_mapView.delegate = self;
_mapView.showsUserLocation = YES;//显示用户位置//定位初始化
[self initLocation];
}
-(void)viewWillAppear:(BOOL)animated
{
[_mapView viewWillAppear];
_mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放
_locService.delegate = self;
[_locService startUserLocationService];//开启定位服务
}
-(void)viewWillDisappear:(BOOL)animated
{
[_mapView viewWillDisappear];
_mapView.delegate = nil; // 不用时,置nil
_locService.delegate = nil;
[_locService stopUserLocationService];//关闭定位服务
}
//定位初始化
-(void) initLocation{
_locService = [[BMKLocationService alloc] init];
//最小定位距离
_locService.distanceFilter = 1;
_locService.desiredAccuracy = kCLLocationAccuracyBest;//定位精度
}
pragma mark-----定位管理------
- (void)willStartLocatingUser{
NSLog(@"开始定位");
} - (void)didStopLocatingUser{
NSLog(@"停止定位");
}
/**
*用户位置更新后,会调用此函数
*@param userLocation 新的用户位置
*/
-
(void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation{
NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);//更新用户位置
[_mapView updateLocationData:userLocation];
} (void)didFailToLocateUserWithError:(NSError *)error
{
NSLog(@"定位失败");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}