IOS百度地图获取所在的城市名称
笔者的app要实现定位所在省和城市名称,借此总结巩固一下!
@interface VenueListVC : BasePageTableViewVC<BMKLocationServiceDelegate,BMKGeoCodeSearchDelegate> { BMKLocationService *_locService; BMKGeoCodeSearch *_geoSearch; float _curLatitude, _curLongitude; NSString *_curCityName,*_curProName;
- (void)viewDidLoad { //百度地图定位 if (![CLLocationManager locationServicesEnabled]) { [UIAlertView showMsg:@"请在“系统设置”中开启定位服务" cancelText:@"确定" cancelAction:^{ [self onBackButtonClick:nil]; } okText:nil okAction:nil]; return; } //初始化BMKLocationService if (_locService == nil) { _locService = [[BMKLocationService alloc]init]; } _locService.delegate = self; //启动LocationService [_locService startUserLocationService]; _geoSearch = [[BMKGeoCodeSearch alloc] init]; //编码服务的初始化(就是获取经纬度,或者获取地理位置服务) _geoSearch.delegate = self;//设置代理为self // [self showProgressHUDWithText:@"正在定位..."]; }
//反向地理编码 -(void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error { if (error == 0) { BMKPointAnnotation* item = [[BMKPointAnnotation alloc]init]; item.coordinate = result.location; item.title = result.address; NSString* titleStr; NSString* showmeg; titleStr = @"反向地理编码"; showmeg = [NSString stringWithFormat:@"%@",item.title]; _curCityName = result.addressDetail.city; _curProName = result.addressDetail.province; NSLog(result); } }
//处理位置坐标更新 - (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation { [_locService stopUserLocationService]; CLLocationCoordinate2D coor; coor=userLocation.location.coordinate; _curLatitude = coor.latitude; _curLongitude = coor.longitude; CLLocationCoordinate2D pt = (CLLocationCoordinate2D){_curLatitude, _curLongitude}; BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc]init]; reverseGeocodeSearchOption.reverseGeoPoint = pt; [_geoSearch reverseGeoCode:reverseGeocodeSearchOption]; } /** *定位失败后,会调用此函数 *@param error 错误号 */ - (void)didFailToLocateUserWithError:(NSError *)error { [_locService stopUserLocationService]; [self hideProgressHUD]; }