问题描述
是否可以在SDK 3.x上反向地理编码您当前的位置?我需要得到当前位置的邮政编码。我看到的唯一的例子使用CoreLocation,我不认为是介绍,直到SDK 4。
Is it possible to reverse Geocode your current location on SDK 3.x? I need to simply get the zip code of the current location. The only examples I've seen use CoreLocation which I dont think was introduced until SDK 4.
推荐答案
你可以使用MKReverseGeocoder从3.0到5.0。因为5.0 MKReverseGeocoder被折旧,建议使用CLGeocoder。
You can use MKReverseGeocoder from 3.0 through 5.0. Since 5.0 MKReverseGeocoder is depreciated and usage of CLGeocoder is advised.
您应该使用CLGeocoder(如果可用)。为了能够提取地址信息,您必须包括地址簿框架。
You should use CLGeocoder if available. In order to be able to extract address information you would have to include Address Book framework.
#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
}
b $ b
MKReverseGeocoder委托方法:
MKReverseGeocoder delegate method:
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
{
//Get zip code
NSString* zipCode = [placemark.addressDictionary objectForKey:(NSString*)kABPersonAddressZIPKey];
}
在iOS 9.0中已弃用MKReverseGeocoder和ABPersonAddressZIPKey。而是可以使用 CLPlacemark
的 postalcode
属性来获取邮政编码:
MKReverseGeocoder and ABPersonAddressZIPKey were deprecated in iOS 9.0. Instead the postalcode
property of the CLPlacemark
can be used to get zip code:
NSString * zipCode = placemark.postalCode;
这篇关于反向地理编码当前位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!