ios百度地图定位实现

附上百度地图基础地图实现:http://blog.csdn.net/fantasy_jun/article/details/53979887

一、定位显示类型

4.1起,新增一种定位方法BMKUserTrackingModeHeading,普通定位+定位罗盘模式。
目前为止,BMKMapView的定位模式(userTrackingMode)有4种分别是:
1) BMKUserTrackingModeNone :普通定位模式,显示我的位置,我的位置图标和地图都不会旋转
2) BMKUserTrackingModeFollow : 定位跟随模式,我的位置始终在地图中心,我的位置图标会旋转,地图不会旋转
3) BMKUserTrackingModeFollowWithHeading : 定位罗盘模式,我的位置始终在地图中心,我的位置图标和地图都会跟着旋转

4) BMKUserTrackingModeHeading:普通定位+定位罗盘模式,显示我的位置,我的位置始终在地图中心,我的位置图标会旋转,地图不会旋转。即在普通定位模式的基础上显示方向。


二、单独获取位置信息(不显示在地图上)

定位功能可以和地图功能分离使用,单独的定位功能使用方式如下:

1.选用#import //引入定位功能所有的头文件

2.添加代理

3.代码部分

-(void)viewDidLoad
{ 
//初始化BMKLocationService 
_locService = [[BMKLocationService alloc]init]; 
_locService.delegate = self; 
//启动LocationService 
[_locService startUserLocationService]; 
} 
//实现相关delegate 处理位置信息更新 
//处理方向变更信息 
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation 
{ 
//NSLog(@"heading is %@",userLocation.heading); 
} 
//处理位置坐标更新 
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation 
{ 
//NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude); 
}


三、展示定位信息(显示在地图上)

1.选用头文件

#import //引入base相关所有的头文件
#import //引入地图功能所有的头文件
#import //引入定位功能所有的头文件
#import //只引入所需的单个头文件

2.添加代理


3.代码部分

.h文件
@interface FJMapVC : UIViewController
{
    BMKMapView* mapView;//地图
    BMKLocationService *_locService;//定位
}
@end
.m文件
@implementation FJMapVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupView];
    
    [self setupLocation];
}

-(void)viewWillAppear:(BOOL)animated
{
    [mapView viewWillAppear];
    mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放
    _locService.delegate = self;
}
-(void)viewWillDisappear:(BOOL)animated
{
    [mapView viewWillDisappear];
    mapView.delegate = nil; // 不用时,置nil
    _locService.delegate = nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)setupView
{
    [self.view setBackgroundColor:[UIColor blueColor]];
    
    mapView = [[BMKMapView alloc]initWithFrame:[UIScreen mainScreen].bounds]; 
    self.view = mapView;

}

//初始化定位
-(void)setupLocation
{
    //自定义精度圈
    BMKLocationViewDisplayParam *param = [[BMKLocationViewDisplayParam alloc] init];
    //线
    param.accuracyCircleStrokeColor = [UIColor colorWithRed:0 green:0 blue:1 alpha:0.5];
    //圈
//    param.accuracyCircleFillColor = [UIColor colorWithRed:0 green:0 blue:1 alpha:0.2];
    [mapView updateLocationViewWithParam:param];
    [mapView setZoomLevel:13];
    
    //初始化BMKLocationService
    _locService = [[BMKLocationService alloc]init];
    //启动LocationService
    [_locService startUserLocationService];
    mapView.showsUserLocation = NO;//先关闭显示的定位图层
    mapView.userTrackingMode = BMKUserTrackingModeFollow;//设置定位的状态
    mapView.showsUserLocation = YES;//显示定位图层
    
}

#pragma mark - 定位代理
/**
 *在地图View将要启动定位时,会调用此函数
 *地图View
 */
- (void)willStartLocatingUser
{
//    NSLog(@"start locate");
}

/**
 *用户方向更新后,会调用此函数
 *@param userLocation 新的用户位置
 */
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation
{
    [mapView updateLocationData:userLocation];
//    NSLog(@"heading is %@",userLocation.heading);
}

/**
 *用户位置更新后,会调用此函数
 *@param userLocation 新的用户位置
 */
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    //    NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
    
    //缺少会导致无法定位
    [mapView updateLocationData:userLocation];
}

/**
 *在地图View停止定位后,会调用此函数
 * 地图View
 */
- (void)didStopLocatingUser
{
//    NSLog(@"stop locate");
}

/**
 *定位失败后,会调用此函数
 *地图View
 *@param error 错误号,参考CLError.h中定义的错误号
 */
- (void)didFailToLocateUserWithError:(NSError *)error
{
    NSLog(@"location error");
}
- (void)dealloc {
    if (mapView) {
        mapView = nil;
    }
}


@end



你可能感兴趣的:(地图开发)