Reverse Geocode Current Location

孤者浪人 提交于 2020-01-09 07:38:11

问题


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.


回答1:


You can use MKReverseGeocoder from 3.0 through 5.0. Since 5.0 MKReverseGeocoder is depreciated and usage of CLGeocoder is advised.

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
}

MKReverseGeocoder delegate method:

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

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;



回答2:


Have a look at http://www.geonames.org. It isn't built into any SDK.




回答3:


In the Question there is some Sample Code on how to get the Latitude and Longitude. Getting Strange Output in Reverse Geo coder in iphone dev




回答4:


CoreLocation was available in SDK 3 (since 2.0) but CLGeocoder (which provides reverse geocode) has been available since 5.0.
Fortunately MapKit framework has MKReverseGeocoder object. This object also provides reverse geocoding (but is obsolete in SDK 5).



来源:https://stackoverflow.com/questions/8705302/reverse-geocode-current-location

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!