您的位置:首页 > 移动开发 > IOS开发

【iOS百度地图系列_1】在地图上定位当前位置

2015-06-01 18:54 260 查看
定位几乎是所有LBS应用必备的一个功能,本文就来讲述一下,百度地图中定位功能的实现。有关基本地图的实现请参考 【iOS百度地图系列_0】百度地图的配置及实现基本地图。

1.用到的代理

<BMKMapViewDelegate,BMKLocationServiceDelegate>

2.在vieDidLoad中

//设置定位精确度,默认:kCLLocationAccuracyBest
    [BMKLocationService setLocationDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
    //指定最小距离更新(米),默认:kCLDistanceFilterNone
    [BMKLocationService setLocationDistanceFilter:100.f];
    
    //初始化BMKLocationService
    _locService = [[BMKLocationService alloc]init];
    _locService.delegate = self;
    //启动LocationService
    [_locService startUserLocationService];


3.BMKLocationServiceDelegate方法
//处理方向变更信息
- (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);
    
    //普通态
    //以下_mapView为BMKMapView对象
    _mapView.showsUserLocation = YES;//显示定位图层
    [_mapView updateLocationData:userLocation];

    CLLocationCoordinate2D coord;
    coord.latitude=userLocation.location.coordinate.latitude;
    coord.longitude=userLocation.location.coordinate.longitude;
    //添加大头针
    BMKPointAnnotation *ann=[[BMKPointAnnotation alloc] init];
    ann.coordinate=coord;
    ann.title=@"你好";
    ann.subtitle=@"我是大头针,我的头很大";
    [_mapView addAnnotation:ann];
    BMKCoordinateRegion region ;//表示范围的结构体
    
    region.center = coord;//指定地图中心点
    region.span.latitudeDelta = 0.1;//经度范围(设置为0.1表示显示范围为0.2的纬度范围)
    region.span.longitudeDelta = 0.1;//纬度范围
    [_mapView setRegion:region animated:YES];
}


4.注意事项

iOS SDK v2.5.0起,为了对iOS8的定位能力做兼容,做了相应的修改,开发者在使用过程中注意事项如下: 需要在info.plist里添加(以下二选一,两个都添加默认使用NSLocationWhenInUseUsageDescription):

NSLocationWhenInUseUsageDescription ,允许在前台使用时获取GPS的描述

NSLocationAlwaysUsageDescription ,允许永久使用GPS的描述
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: