反向地理编码当前位置

5

在SDK 3.x上,是否可以对当前位置进行逆向地理编码?我只需要获取当前位置的邮政编码。我看到的所有示例都使用CoreLocation,但我认为这是在SDK 4之后引入的。

4个回答

10

您可以从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已弃用。可以使用CLPlacemarkpostalcode属性来获取邮政编码:

NSString * zipCode = placemark.postalCode;

0

你能否使用iPhone的当前位置与此Web服务进行关联? - savagenoob
当然可以。只需提供纬度/经度即可。 - Daniel A. White

0

0

CoreLocation在SDK 3中可用(自2.0以来),但CLGeocoder(提供反向地理编码)自5.0以来可用。
幸运的是,MapKit框架有MKReverseGeocoder对象。该对象也提供反向地理编码(但在SDK 5中已过时)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接