美文网首页
百度地图,反地理编码

百度地图,反地理编码

作者: Archer_S | 来源:发表于2016-12-16 16:12 被阅读361次

效果展示:


城市定位反地理编码.png
图中红色框中的地址是根据手机的位置定位出来的,定位使用的是百度地图SDK的第三方。
  1. 引入头文件等
  2. 在要用到定位的控制器的.h文件中设置相关协议以及属性
@interface WFCityChooseController : UIViewController<
BMKGeneralDelegate,
BMKMapViewDelegate,
BMKLocationServiceDelegate,
BMKGeoCodeSearchDelegate
>
{
    BMKGeoCodeSearch *getCodeSearch;
    BMKLocationService *locationService;
}
  1. 在视图将要出现和将要消失的时候分别开启定位服务后关闭定位服务
  -(void)viewWillAppear:(BOOL)animated
  {
     [locationService startUserLocationService];
     locationService.delegate=self;  
     getCodeSearch.delegate = self;
 }
 -(void)viewWillDisappear:(BOOL)animated
 {
     [locationService stopUserLocationService];
     locationService.delegate=nil; 
     getCodeSearch.delegate = nil; 

 }
}
  1. 通过协议方法获得城市名
#pragma mark调百度地图的类,完成反地理编码
-(void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation{
    //完成地理反编码
    //1.创建反向地理编码选项对象
    BMKReverseGeoCodeOption *reverseOption=[[BMKReverseGeoCodeOption alloc]init];
    //2.给反向地理编码选项对象的坐标点赋值
    reverseOption.reverseGeoPoint=userLocation.location.coordinate;
    //3.执行反地理编码
    [getCodeSearch reverseGeoCode:reverseOption];
}
-(void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
{
    BMKAddressComponent *component=[[BMKAddressComponent alloc]init];
    component=result.addressDetail;
    self.cityLabel.text=component.city;
}

相关文章

网友评论

      本文标题:百度地图,反地理编码

      本文链接:https://www.haomeiwen.com/subject/hpinmttx.html