iOS开发-开发百度地图2-iOS SDK 3.0.0-定位及geo检索服务

来源:互联网 发布:高中免费教学视频软件 编辑:程序博客网 时间:2024/06/08 00:27

四、定位功能

    由于系统原因,iOS不允许使用第三方定位,因此地图SDK中的定位方法,本质上是对原生定位的二次封装。通过封装,开发者可更便捷的使用。此外,地图SDK中还提供了相应的定位图层(支持定位三态效果),帮助开发者显示当前位置信息。

     注:自iOS8起,系统定位功能进行了升级,SDK为了实现最新的适配,自v2.5.0起也做了相应的修改,开发者在使用定位功能之前,需要在info.plist里添加(以下二选一,两个都添加默认使用NSLocationWhenInUseUsageDescription):

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

NSLocationAlwaysUsageDescription ,允许永久使用GPS的描述



1、签协议,声明属性

@interface YMMapViewController

<BMKLocationServiceDelegate,BMKMapViewDelegate>

@property (nonatomic,retain)BMKLocationService *locService;

@property (nonatomic,retain)BMKMapView *mapView;


2、具体实现

- (void)viewDidLoad {

    [superviewDidLoad];

    _mapView = [[BMKMapViewalloc]initWithFrame:CGRectMake(0,0,320,480)];

    _mapView.trafficEnabled =YES;

//    设置路况信息

    self.view =_mapView;

//    设置地图的比例

    _mapView.zoomLevel =20;

//    设置地图旋转角度

    _mapView.rotation =50;

    _locService = [[BMKLocationServicealloc]init];

    _locService.delegate =self;

    //启动LocationService

    [_locServicestartUserLocationService];

}

//实现相关delegate处理位置信息更新

//定位成功处理方法

- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation

{

    NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);

    _mapView.showsUserLocation =YES;//显示定位图层

    [_mapViewupdateLocationData:userLocation];

    _mapView.centerCoordinate = userLocation.location.coordinate;

//    设置地图比例 

    _mapView.zoomLevel =20;


}

//定位失败处理方法

- (void)didFailToLocateUserWithError:(NSError *)error{

  UIAlertView* alert = [[UIAlertViewalloc]

                              initWithTitle:@"提示"

                              message:@"定位失败,请设置允许软件开始定位"

                              delegate:nil

                              cancelButtonTitle:@"确定"

                              otherButtonTitles:nil];

        [alert show];  

}


-(void)viewWillDisappear:(BOOL)animated {

    [superviewWillDisappear:animated];

    [_mapViewviewWillDisappear];

    _mapView.delegate =nil;// // 此处记得不用的时候需要置nil,否则影响内存的释放

    _locService.delegate =nil;

}


- (void)viewWillAppear:(BOOL)animated {

    [superviewWillAppear:animated];

    [_mapViewviewWillAppear];

    _mapView.delegate =self;// 此处记得不用的时候需要置nil,否则影响内存的释放

    _locService.delegate =self;


}


五、geo检索服务

1、签协议,声明属性

<BMKGeoCodeSearchDelegate>{

BMKGeoCodeSearch* _geocodesearch;

}


- (void)viewDidLoad {

 _geocodesearch = [[BMKGeoCodeSearchalloc]init];

 _geocodesearch.delegate = self;


}

2、在定位成功的方法里调用此方法

//定位成功方法

- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation;

- (void)nowPlace{

    isGeoSearch =false;

    CLLocationCoordinate2D pt = (CLLocationCoordinate2D){0,0};

    pt = (CLLocationCoordinate2D){_mapView.centerCoordinate.latitude ,_mapView.centerCoordinate.longitude};

    

    BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOptionalloc]init];

    reverseGeocodeSearchOption.reverseGeoPoint = pt;

    

    BOOL flag = [_geocodesearchreverseGeoCode:reverseGeocodeSearchOption];

    if(flag)

    {

        NSLog(@"geo检索发送成功");

    }

    else

    {

        NSLog(@"geo检索发送失败");

    }

}

#pragma mark - BMKGeoCodeSearchDelegate协议方法

-(void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error

{

    BMKAddressComponent *addressComponent = result.addressDetail;

    NSString *title = [NSStringstringWithFormat:@"%@%@%@%@", addressComponent.city, addressComponent.district, addressComponent.streetName, addressComponent.streetNumber];

    NSLog(@"%s -- %@",__func__, title);

    

    

}







0 0
原创粉丝点击