1. 百度的标配
AppDelegate<BMKGeneralDelegate>代理
#import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相关所有的头文件
#import <BaiduMapAPI_Map/BMKMapComponent.h>
{
BMKMapManager* _mapManager;创建管理者
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
_mapManager = [[BMKMapManager alloc]init];
// 如果要关注网络及授权验证事件,请设定 generalDelegate参数
BOOL ret = [_mapManager start:@"你申请到的key" generalDelegate:nil];
if (!ret) {
NSLog(@"manager start failed!");
}
}
1.1.创建地图界面<BMKMapViewDelegate>
1.2.导入头文件
#import<BaiduMapAPI_Map/BMKMapView.h>//只引入所需的单个头文件
@property(nonatomic,strong)BMKMapView* mapView;
- (void)viewDidLoad {
[super viewDidLoad];
_mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 0,320, 480)];(根据需要创建大小)
[self.view addSubview:_mapView];
}
1.3.需要注意
-(void)viewWillAppear:(BOOL)animated
{
[_mapView viewWillAppear];
_mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放
}
-(void)viewWillDisappear:(BOOL)animated
{
[_mapView viewWillDisappear];
_mapView.delegate = nil; // 不用时,置nil
}
!!!以下都是在创建地图之后流程
2.1.创建地图定位<BMKMapViewDelegate,BMKLocationServiceDelegate>
2.2.导入头文件
#import <BaiduMapAPI_Map/BMKMapView.h>//只引入所需的单个头文件
#import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地图功能所有的头文件
#import <BaiduMapAPI_Location/BMKLocationComponent.h>//引入定位功能所有的头文件
{
BMKLocationService* _locService;
BMKPointAnnotation* pointAnnotation;
}
@property (nonatomic,strong) BMKMapView* mapView;
@property (nonatomic,assign) BOOL isfirst;
/*++++++++++++++++++++++++++++++创建定位+++++++++++++++++++++++++++++++++++++++++++++++*/
- (void)viewDidLoad {
[super viewDidLoad]; /*需要先创建地图*/
_mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)]; /**设置地图级别*/
[_mapView setZoomLevel:16];
/**适配ios7*/
if( ([[[UIDevice currentDevice] systemVersion] doubleValue]>=7.0)) { self.navigationController.navigationBar.translucent = NO; }
_locService = [[BMKLocationService alloc]init];
_locService.delegate = self;
[_locService startUserLocationService];//开始定位
/*4.1起,新增一种定位方法BMKUserTrackingModeHeading,普通定位+定位罗盘模式。 目前为止,BMKMapView的定位模式(userTrackingMode)有4种分别是: 1) BMKUserTrackingModeNone :普通定位模式,显示我的位置,我的位置图标和地图都不会旋转 2) BMKUserTrackingModeFollow :定位跟随模式,我的位置始终在地图中心,我的位置图标会旋转,地图不会旋转 3) BMKUserTrackingModeFollowWithHeading :定位罗盘模式,我的位置始终在地图中心,我的位置图标和地图都会跟着旋转 4) BMKUserTrackingModeHeading:普通定位+定位罗盘模式,显示我的位置,我的位置始终在地图中心,我的位置图标会旋转,地图不会旋转。即在普通定位模式的基础上显示方向。 _mapView.userTrackingMode = BMKUserTrackingModeFollow;//设置定位的状态 */
_mapView.userTrackingMode = BMKUserTrackingModeFollow;//设置定位的状态
_mapView.showsUserLocation = YES;//显示定位图层
[self.view addSubview:_mapView];
}
/*++++++++++++++++++++++++++++++定位需要(有些非必须)+++++++++++++++++++++++++++++++++++++++++++++++*/
/**
*在地图View将要启动定位时,会调用此函数 *@param mapView 地图View */
- (void)willStartLocatingUser
{
NSLog(@"start locate");
}
/** *用户方向更新后,会调用此函数 *@param userLocation 新的用户位置 */
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation
{
[_mapView updateLocationData:userLocation];
}
/** *用户位置更新后,会调用此函数 *@param userLocation 新的用户位置 */
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
[_mapView updateLocationData:userLocation];
if (_isfirst == YES) { //那么定位点置中
self.mapView.centerCoordinate = _locService.userLocation.location.coordinate;
}
[_locService stopUserLocationService];
}
/** *在地图View停止定位后,会调用此函数 *@param mapView 地图View */
- (void)didStopLocatingUser
{
NSLog(@"stop locate");
}
/** *定位失败后,会调用此函数 *@param mapView 地图View *@param error 错误号,参考CLError.h中定义的错误号 */
- (void)didFailToLocateUserWithError:(NSError *)error
{
NSLog(@"location error");
}
/*++++++++++++++++++++++++++++++必备+++++++++++++++++++++++++++++++++++++++++++++++*/
- (void)dealloc {
if (_mapView) {
_mapView = nil;
}
}
-(void)viewWillAppear:(BOOL)animated
{
[_mapView viewWillAppear];
_mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放
}
-(void)viewWillDisappear:(BOOL)animated {
[_mapView viewWillDisappear];
_mapView.delegate = nil; // 不用时,置nil
_locService.delegate = nil;
}
2.3.增加
- (void)customLocationAccuracyCircle
{
/*+++++++++++++++++++++++自定义精度圈+++++++++++++++++++++++++++*/
/**光圈外边框的颜色**/
BMKLocationViewDisplayParam *param = [[BMKLocationViewDisplayParam alloc] init];
param.accuracyCircleStrokeColor = [UIColor purpleColor]; /**光圈的填充侧**/ param.accuracyCircleFillColor = [UIColor redColor];
[_mapView updateLocationViewWithParam:param];}
/*+++++++++++++++++++++++添加大头针+++++++++++++++++++++++++++*/-(void)addpointAnnotation
{
pointAnnotation = [[BMKPointAnnotation alloc]init];
CLLocationCoordinate2D coor;
coor.latitude = 34;
coor.longitude = 113;
pointAnnotation.coordinate = coor;
pointAnnotation.title = @"test";
pointAnnotation.subtitle = @"此Annotation可拖拽!";
[_mapView addAnnotation:pointAnnotation];
}
/*+++++++++++++++++++++++自定义大头针(更换大头针)+++++++++++++++++++++++++++*/
- (BMKAnnotationView *)mapView:(BMKMapView *)view viewForAnnotation:(id <BMKAnnotation>)annotation
{
NSString *AnnotationViewID = [NSString stringWithFormat:@"renameMark"]; BMKPinAnnotationView*newAnnotation = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
// 设置颜色
((BMKPinAnnotationView*)newAnnotation).pinColor = BMKPinAnnotationColorPurple;
// 从天上掉下效果
((BMKPinAnnotationView*)newAnnotation).animatesDrop = NO;
// 设置可拖拽 //
((BMKPinAnnotationView*)newAnnotation).draggable = YES;
//设置大头针图标
((BMKPinAnnotationView*)newAnnotation).image = [UIImage imageNamed:@"biao"]; return newAnnotation;
}
网友评论