在SDK 3.x上,是否可以对当前位置进行逆向地理编码?我只需要获取当前位置的邮政编码。我看到的所有示例都使用CoreLocation,但我认为这是在SDK 4之后引入的。
在SDK 3.x上,是否可以对当前位置进行逆向地理编码?我只需要获取当前位置的邮政编码。我看到的所有示例都使用CoreLocation,但我认为这是在SDK 4之后引入的。
您可以从3.0到5.0使用MKReverseGeocoder。自5.0以来,MKReverseGeocoder已被弃用,并建议使用CLGeocoder。
如果可用,应该使用CLGeocoder。为了能够提取地址信息,您需要包含Address Book框架。
#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];
}
iOS 9.0中MKReverseGeocoder和ABPersonAddressZIPKey已弃用。可以使用CLPlacemark
的postalcode
属性来获取邮政编码:
NSString * zipCode = placemark.postalCode;
看一下http://www.geonames.org。它没有集成到任何SDK中。
CoreLocation在SDK 3中可用(自2.0以来),但CLGeocoder(提供反向地理编码)自5.0以来可用。
幸运的是,MapKit框架有MKReverseGeocoder对象。该对象也提供反向地理编码(但在SDK 5中已过时)。