How to obtain the complete zip code through the mobile phone GPS

2

I'm trying to get the full zip code through my location using GPS from my cell in Objective-C , but it returns only 5 digits. I used the following code snippet:

NSString *cepcompleto = [NSString stringWithFormat:@"%@-%@", placemark.postalCode, [placemark.addressDictionary objectForKey:@"PostCodeExtension"]];

The rest of the code follows:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    CLLocation *currentLocation = newLocation;
    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
        if (error == nil && [placemarks count] > 0) {
            placemark = [placemarks lastObject];
            NSString *cepcompleto = [NSString stringWithFormat:@"%@-%@", placemark.postalCode, [placemark.addressDictionary objectForKey:@"PostCodeExtension"]];

            digitoCep = [placemark.addressDictionary objectForKey:@"PostCodeExtension"];
            cepEstatico = cepcompleto;

            [vg setCep];
        } else {
            NSLog(@"%@", error.debugDescription);
        }
    }];
    [manager startUpdatingLocation];
}
    
asked by anonymous 17.03.2015 / 19:17

1 answer

2

Your code is correct. However, CEP mapping in Brazil is flawed and you may encounter null values in the case of key @"PostCodeExtension" . For this reason, I consider a safer alternative to use the key @"FormattedAddressLines" .

Instead of:

NSString *cepcompleto = [NSString stringWithFormat:@"%@-%@", placemark.postalCode, [placemark.addressDictionary objectForKey:@"PostCodeExtension"]];

Try this:

NSString *cepcompleto = [NSString stringWithFormat:@"%@", [placemark.addressDictionary[@"FormattedAddressLines"][3]];

Note that the key @"FormattedAddressLines" is a NSArray with other values such as address, neighborhood and other values besides the ZIP code that are formatted similarly to what we find in Brazil.

    
30.09.2015 / 22:29