【问题标题】:Reverse Geocode Current Location反向地理编码当前位置
【发布时间】:2012-01-02 21:02:56
【问题描述】:

是否可以在 SDK 3.x 上对您当前的位置进行反向地理编码?我需要简单地获取当前位置的邮政编码。我见过的唯一示例使用 CoreLocation,我认为它直到 SDK 4 才引入。

【问题讨论】:

    标签: objective-c cocoa-touch iphone-sdk-3.0 reverse-geocoding


    【解决方案1】:

    您可以从 3.0 到 5.0 使用 MKReverseGeocoder。由于 MKReverseGeocoder 5.0 已弃用,建议使用 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 中已弃用。相反,CLPlacemark 的 postalcode 属性可用于获取邮政编码:

    NSString * zipCode = placemark.postalCode;
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 你能用 iPhone 当前的位置绑定到这个 web 服务吗?
      • 绝对。只需提供纬度/经度即可。
      【解决方案3】:

      在问题中有一些关于如何获取纬度和经度的示例代码。 Getting Strange Output in Reverse Geo coder in iphone dev

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-06-30
          • 1970-01-01
          • 1970-01-01
          • 2015-02-14
          • 2023-04-09
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多