萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> iOS 開發

iOS 8 逆地理編碼實戰:經緯度轉詳細地址(國家/省/市/街道/郵編)

核心流程

  1. 獲取經緯度(CLLocationManager)
  2. 構造 CLLocation 對象
  3. 調用 CLGeocoder.reverseGeocodeLocation:completionHandler:
  4. 解析 CLPlacemark 獲取結構化地址

完整代碼實現

@interface LocationManager : NSObject 
@property (strong, nonatomic) CLLocationManager *locationManager;
@property (copy) void (^completion)(NSString *address, NSError *error);
@end

@implementation LocationManager

- (void)startLocatingWithCompletion:(void (^)(NSString *, NSError *))completion {
    self.completion = completion;
    
    _locationManager = [[CLLocationManager alloc] init];
    _locationManager.delegate = self;
    _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    _locationManager.distanceFilter = 10.0;
    
    // iOS 8+ 必須顯式請求授權
    if ([_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
        [_locationManager requestWhenInUseAuthorization];
    }
    [_locationManager startUpdatingLocation];
}

#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    CLLocation *location = locations.lastObject;
    NSLog(@"經度:%f,緯度:%f,海拔:%f", location.coordinate.longitude, location.coordinate.latitude, location.altitude);
    
    [self reverseGeocodeLocation:location];
    [manager stopUpdatingLocation]; // 單次定位即停止
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    self.completion(nil, error);
}

- (void)reverseGeocodeLocation:(CLLocation *)location {
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
        if (error) {
            self.completion(nil, error);
            return;
        }
        CLPlacemark *pm = placemarks.firstObject;
        // 組裝詳細地址
        NSString *address = [NSString stringWithFormat:@"%@%@%@%@%@",
            pm.country ?: @"",           // 國家
            pm.administrativeArea ?: @"", // 省/直轄市
            pm.locality ?: @"",          // 市
            pm.subLocality ?: @"",       // 區/縣
            pm.thoroughfare ?: @"",      // 街道
            pm.subThoroughfare ?: @""    // 號
        ];
        NSLog(@"詳細地址:%@", address);
        NSLog(@"郵編:%@", pm.postalCode ?: @"無");
        NSLog(@"ISO 國家碼:%@", pm.ISOcountryCode ?: @"無");
        self.completion(address, nil);
    }];
}
@end

CLPlacemark 關鍵屬性速查

屬性含義示例
country國家名中國
ISOcountryCodeISO 3166-1 國家碼CN
administrativeArea省/自治區/直轄市北京市
subAdministrativeArea地級市(省下級)北京市
locality區/縣級市朝陽區
subLocality街道/鄉鎮建國門街道
thoroughfare道路名建國路
subThoroughfare門牌號88號
postalCode郵政編碼100020
namePOI 名稱國貿大廈
areasOfInterest周邊熱門區域[@"CBD", @"國貿"]

重要注意事項

  • 網絡請求:逆地理編碼需聯網,必須異步處理,UI 給出加載狀態
  • 頻率限制:Apple 服務端有請求頻率限制,避免短時間大量調用
  • 中文地址:系統按設備語言返回中文,無需額外處理
  • iOS 8 權限:Info.plist 必須配置 NSLocationWhenInUseUsageDescription,否則無回調
  • 舊 API 棄用:MKReverseGeocoder 自 iOS 8 起棄用,務必遷移至 CLGeocoder
copyright © 萬盛學電腦網 all rights reserved