效果展示
![城市定位反地理编码.png](https://img-blog.csdn.net/20161216161752804?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvQXJjaGVyX3Nj/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
图中红色框中的地址是根据手机的位置定位出来的,定位使用的是百度地图SDK的第三方。
- 引入头文件等
- 在要用到定位的控制器的.h文件中设置相关协议以及属性
@interface WFCityChooseController : UIViewController<
BMKGeneralDelegate,
BMKMapViewDelegate,
BMKLocationServiceDelegate,
BMKGeoCodeSearchDelegate
>
{
BMKGeoCodeSearch *getCodeSearch;
BMKLocationService *locationService;
}
- 在视图将要出现和将要消失的时候分别开启定位服务后关闭定位服务
-(void)viewWillAppear:(BOOL)animated
{
[locationService startUserLocationService];
locationService.delegate=self;
getCodeSearch.delegate = self;
}
-(void)viewWillDisappear:(BOOL)animated
{
[locationService stopUserLocationService];
locationService.delegate=nil;
getCodeSearch.delegate = nil;
}
}
#pragma mark调百度地图的类,完成反地理编码
-(void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation{
BMKReverseGeoCodeOption *reverseOption=[[BMKReverseGeoCodeOption alloc]init];
reverseOption.reverseGeoPoint=userLocation.location.coordinate;
[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;
}