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

本文介绍如何在iOS应用中使用地图SDK实现定位功能,包括配置定位权限、实现定位图层显示及处理定位成功与失败的情况。同时,还介绍了如何进行Geo检索服务,包括反向Geo检索获取当前位置详细地址信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

四、定位功能

    由于系统原因,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);

    

    

}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值