How to get the complete zip using reverseGeocodeLocation in xcode?

8

I am using reverseGeocodeLocation to fetch the zip code and it works fine; however, it only has 5 digits of zip code. How to get the full ZIP code? Here is the code:

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation {

    CLLocationCoordinate2D coords = newLocation.coordinate;

    lastLat = coords.latitude;
    lastLng = coords.longitude;

    CLGeocoder *reverse = [[CLGeocoder alloc] init];
CLLocation *location = [[CLLocation alloc] initWithLatitude:coords.latitude       longitude:coords.longitude];
    lastAccuracy = newLocation.horizontalAccuracy;
    lblAccuracy.text = [NSString stringWithFormat:@"%f",lastAccuracy];
    if(lastAccuracy<=10)
    {
    [reverse reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
        if ([placemarks count]>0) {
            CLPlacemark *place = [placemarks objectAtIndex:0];
            strCEP = place.postalCode;
            strLastSubLocation = place.subLocality;
            strLastLocation = place.locality;
            strEndereco = place.thoroughfare;
            lblEndereco.text = strEndereco;
            strLastLocation = [strLastLocation stringByReplacingOccurrencesOfString:@"Saõ" withString:@"São"];
        }
    }];
}
    
asked by anonymous 16.04.2014 / 00:15

1 answer

6

In addition to the discrete properties, the CLPlacemark class defines a dictionary containing the address according to the keys defined by the AddressBook framework. To inspect the dictionary, enter the line below shortly after getting place :

NSLog(@"Address dictionary: %@", place.addressDictionary);

You will see that there is a key called PostCodeExtension which, when it exists, contains the additional three digits of the zip code. The value is of type NSString , so you can concatenate it to place.postalCode . To get this extension, you can write something like

NSString *strExtensaoCEP = place.addressDictionary[@"PostCodeExtension"];

or

NSString *strExtensaoCEP = [place.addressDictionary objectForKey:@"PostCodeExtension"];

There is one however: na ABPerson reference , there is no constant for PostCodeExtension . In the case of the CEP, for example, there is the constant kABPersonAddressZIPKey . This may mean that there is no formal support for this constant and, for example, it may no longer exist in future releases. Internally, the GeoServices framework contains a property named postCodeExtension , then maybe this is already a stable field. It may also be the case that there are plans to create a constant or that they just have forgotten about it. I suggest you open a radar by requesting an equivalent constant and write your code keeping in mind that this key can be renamed or cease to exist.

    
19.04.2014 / 09:30