1.参考百度地图API官网
http://developer.baidu.com/map/sdkiosdev-2.htm
2.导入头文件,库文件,资源文件,和framework
详情见官网说明
我在引入库文件的时候使用的是百度提供的第二种方法,第三种方法测试过但未成功。
注:静态库中采用ObjectC++实现,因此需要您保证您工程中至少有一个.mm后缀的源文件(您可以将任意一个.m后缀的文件改名为.mm),或者在工程属性中指定编译方式,即将Xcode的Project -> Edit Active Target -> Build -> GCC4.2 - Language -> Compile Sources As设置为"Objective-C++"
如果使用Xcode5开发基于iOS7的百度地图应用时,请将Xcode5的Project -> Edit Active Target -> Build -> Linking -> Other Linker Flags中的“-ObjC”修改为“-all_load”
3.在需要使用百度地图的视图控制类中要执行如下操作
#import "BMapKit.h"
4.添加视图view及相关设置
mapView = [[BMKMapView alloc]initWithFrame:self.view.bounds];
//设置地图旋转角度
//mapView.rotation = 90;
//设置地图俯视角度
//mapView.overlooking = -30;
//设置地图事件的回调协议,若不加入此句则不会发生回调
mapView.delegate = self;
//打开实时路况图层
[mapView setMapType:BMKMapTypeTrafficOn];
//关闭实时路况图层
//[mapView setMapType:BMKMapTypeStandard];
//设置按钮,添加点击定位事件
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(270, 65, 50, 50);
[btn setTitle:@"定位" forState:UIControlStateNormal] ;
[btn addTarget:self action:@selector(locateSelf:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:mapView];
[self.view addSubview:btn];
/**
*点中底图标注后会回调此接口
*@param mapview 地图View
*@param mapPoi 标注点信息
*/
- (void)mapView:(BMKMapView *)mapView onClickedMapPoi:(BMKMapPoi*)mapPoi
{
NSLog(@"%@",mapPoi.text);
}
5.定位功能的实现
注:实现定位功能回调必须实现bmkmapviewdalegate协议
mapView.delegate = self;
相关回调:
/*定位调用函数
将要启动定位调用mapViewWillStartLocatingUser
定位成功后,调用didUpdateUserLocation
停止定位后,调用mapViewDidStopLocatingUser
定位失败,调用didFailToLocateUserWithError
*/
-(void)locateSelf:(id)sender
{
//先将地图定位图层还原默认
mapView.showsUserLocation = NO;
/* 设置userTrackingMode
BMKUserTrackingModeNone = 0, /// 普通定位模式
BMKUserTrackingModeFollow, /// 定位跟随模式
BMKUserTrackingModeFollowWithHeading, /// 定位罗盘模式
*/
mapView.userTrackingMode = BMKUserTrackingModeFollowWithHeading;
//设置地图定位图层
mapView.showsUserLocation = YES;
}
/**
*在地图View将要启动定位时,会调用此函数
*@param mapView 地图View
*/
- (void)mapViewWillStartLocatingUser:(BMKMapView *)mapView
{
NSLog(@"start locate");
}
/**
*用户位置更新后,会调用此函数
*@param mapView 地图View
*@param userLocation 新的用户位置
*/
- (void)mapView:(BMKMapView *)mapView didUpdateUserLocation:(BMKUserLocation *)userLocation
{
if (userLocation != nil) {
NSLog(@"%f %f", userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude);
}
}
/**
*在地图View停止定位后,会调用此函数
*@param mapView 地图View
*/
- (void)mapViewDidStopLocatingUser:(BMKMapView *)mapView
{
NSLog(@"stop locate");
}
/**
*定位失败后,会调用此函数
*@param mapView 地图View
*@param error 错误号,参考CLError.h中定义的错误号
*/
- (void)mapView:(BMKMapView *)mapView didFailToLocateUserWithError:(NSError *)error
{
NSLog(@"location error%@",error);
}