效果展示:
图中红色框中的地址是根据手机的位置定位出来的,定位使用的是百度地图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{
//完成地理反编码
//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;
}