已棄用:MKReverseGeocoder (iOS 8 棄用),請遷移至 CLGeocoder。
@interface GeoCoderHelper : NSObject
+ (void)reverseGeocodeLocation:(CLLocation *)location
completion:(void (^)(NSString *formattedAddress, CLPlacemark *placemark, NSError *error))completion;
@end
@implementation GeoCoderHelper
+ (void)reverseGeocodeLocation:(CLLocation *)location completion:(void (^)(NSString *, CLPlacemark *, NSError *))completion {
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
if (error) {
completion(nil, nil, error);
return;
}
CLPlacemark *placemark = placemarks.firstObject;
NSString *address = [self formattedAddressFromPlacemark:placemark];
completion(address, placemark, nil);
}];
}
+ (NSString *)formattedAddressFromPlacemark:(CLPlacemark *)placemark {
// 中國地址格式:國家 省 市 區 街道 號
return [NSString stringWithFormat:@"%@%@%@%@%@%@",
placemark.country ?: @"",
placemark.administrativeArea ?: @"",
placemark.locality ?: @"",
placemark.subLocality ?: @"",
placemark.thoroughfare ?: @"",
placemark.subThoroughfare ?: @""];
}
// 便捷方法:直接經緯度
+ (void)reverseGeocodeLatitude:(CLLocationDegrees)lat longitude:(CLLocationDegrees)lon completion:(void (^)(NSString *, CLPlacemark *, NSError *))completion {
CLLocation *loc = [[CLLocation alloc] initWithLatitude:lat longitude:lon];
[self reverseGeocodeLocation:loc completion:completion];
}
@end
| 屬性 | 中文含義 | 示例 (北京國貿) |
|---|---|---|
name | POI 名稱 | 中國國際貿易中心 |
country | 國家 | 中國 |
ISOcountryCode | ISO 國家碼 | CN |
administrativeArea | 省/直轄市/自治區 | 北京市 |
subAdministrativeArea | 地級市(省轄) | 北京市 |
locality | 區/縣級市 | 朝陽區 |
subLocality | 街道/鄉鎮 | 建國門街道 |
thoroughfare | 道路名 | 建國路 |
subThoroughfare | 門牌號 | 88號 |
postalCode | 郵政編碼 | 100020 |
areasOfInterest | 商圈/地標 | [@"CBD", @"國貿"] |
inlandWater | 內陸水體 | — |
ocean | 海洋 | — |