5

是否有可能将您的当前位置逆向地理编码到SDK 3.x上?我需要简单地获取当前位置的邮政编码。我见过的唯一例子是使用CoreLocation,我认为它是在SDK 4之前推出的。逆向地理编码当前位置

回答

10

您可以使用从3.0到5.0的MKReverseGeocoder。由于5.0 MKReverseGeocoder已折旧并建议使用CLGeocoder。

如果可用,应该使用CLGeocoder。为了能够提取地址信息,您必须包含地址簿框架。

#import <AddressBookUI/AddressBookUI.h> 
#import <CoreLocation/CLGeocoder.h> 
#import <CoreLocation/CLPlacemark.h> 

- (void)reverseGeocodeLocation:(CLLocation *)location 
{ 
    CLGeocoder* reverseGeocoder = [[CLGeocoder alloc] init]; 
    if (reverseGeocoder) { 
     [reverseGeocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) { 
      CLPlacemark* placemark = [placemarks firstObject]; 
      if (placemark) { 
       //Using blocks, get zip code 
       NSString* zipCode = [placemark.addressDictionary objectForKey:(NSString*)kABPersonAddressZIPKey]; 
      } 
     }]; 
    }else{ 
     MKReverseGeocoder* rev = [[MKReverseGeocoder alloc] initWithCoordinate:location.coordinate]; 
     rev.delegate = self;//using delegate 
     [rev start]; 
     //[rev release]; release when appropriate 
    } 
    //[reverseGeocoder release];release when appropriate 
} 

MKReverseGeocoder委托方法:

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark 
{ 
    //Get zip code 
    NSString* zipCode = [placemark.addressDictionary objectForKey:(NSString*)kABPersonAddressZIPKey]; 
} 

MKReverseGeocoder和ABPersonAddressZIPKey分别在IOS 9.0弃用。取而代之的是CLPlacemarkpostalcode属性可以用来获得邮政编码:

NSString * zipCode = placemark.postalCode; 
+0

工作完美,谢谢! – RyanG 2012-08-07 15:20:04

0

看看http://www.geonames.org。它没有内置到任何SDK中。

+0

您可以使用iPhone手机当前位置扎入这个web服务? – savagenoob 2012-01-02 21:27:03

+0

当然。只要提供它的经度/纬度。 – 2012-01-02 21:29:07

0

CoreLocation在SDK 3(自2.0开始)中可用,但CLGeocoder(提供反向地理编码)自5.0版本开始可用。
幸好MapKit框架有MKReverseGeocoder对象。该对象还提供反向地理编码(但在SDK 5中已过时)。