1.百度地圖定位
- 需要調(diào)用代理方法設(shè)置
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
, 從代理方法里獲取當(dāng)前最新的location.
- 創(chuàng)建
BMKGeoCodeSearch
進(jìn)行反地理編碼,記得掛代理<BMKGeoCodeSearchDelegate>
self.getCodeSearch = [[BMKGeoCodeSearch alloc] init];
self.getCodeSearch.delegate = self;
//1.創(chuàng)建反向地理編碼選項(xiàng)對象
BMKReverseGeoCodeOption *reverseOption=[[BMKReverseGeoCodeOption alloc]init];
//2.給反向地理編碼選項(xiàng)對象的坐標(biāo)點(diǎn)賦值
reverseOption.reverseGeoPoint=userLocation.location.coordinate;
//3.執(zhí)行反地理編碼
[self.getCodeSearch reverseGeoCode:reverseOption];
- 執(zhí)行代理方法,獲取反地理編碼
-(void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
{
BMKAddressComponent *component=[[BMKAddressComponent alloc]init];
component=result.addressDetail;
[self.locationBtn setTitle:[NSString stringWithFormat:@"%@%@", component.streetName?:@"", component.streetNumber?:@""] forState:0];
}
- 補(bǔ)充說明
使用對應(yīng)的地圖獲取出來的經(jīng)緯度,只能使用對應(yīng)的正向/反向地理編碼進(jìn)行解析, 以上是百度地圖解析地理編碼的方法
2.系統(tǒng)自帶方法
1.設(shè)置參數(shù)
- (void)reveLocation
{
//定位初始化
CLLocationManager * locationManager=[[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = 10;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager requestWhenInUseAuthorization];
[locationManager startUpdatingLocation];
}
2.實(shí)現(xiàn)回調(diào),執(zhí)行代理方法
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
//此處locations存儲了持續(xù)更新的位置坐標(biāo)值簇宽,取最后一個(gè)值為最新位置,如果不想讓其持續(xù)更新位置,則在此方法中獲取到一個(gè)值之后讓locationManager stopUpdatingLocation
CLLocation *currentLocation = [locations lastObject];
// 獲取當(dāng)前所在的城市名
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
//根據(jù)經(jīng)緯度反向地理編譯出地址信息
[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *array, NSError *error)
{
if (!error)
{
CLPlacemark *place = array.lastObject;
_locationName = place.name;
}
else
{
}
}];
}
3.高德地圖
1.設(shè)置參數(shù),進(jìn)行逆地理
- (void)searchReGeocode
{
AMapReGeocodeSearchRequest *regeoRequest = [[AMapReGeocodeSearchRequest alloc] init];
regeoRequest.searchType = AMapSearchType_ReGeocode;
regeoRequest.location = [AMapGeoPoint locationWithLatitude:39.990459 longtitude:116.481476];
regeoRequest.radius = 10000;
regeoRequest.requireExtension = YES;
[self.search AMapReGoecodeSearch: regeoRequest];
}
2.實(shí)現(xiàn)回調(diào)祭钉,獲取查詢結(jié)果:
- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response
{
NSString *result = [NSString stringWithFormat:@"ReGeocode: %@", response.regeocode];
NSLog(@"ReGeo: %@", result);
}