How to get the full CEP using reverseGeocodeLocation on Xcode?

Asked

Viewed 1,135 times

8

I’m using the reverseGeocodeLocation to search for the zip code and it works well; however, it only brings 5 digits of the zip code. How to get the full zip code? Follow 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"];
        }
    }];
}

1 answer

6


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

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

You’ll see there’s a key called PostCodeExtension which, if any, contains the three additional digits of the CEP. The value is of the NSString, then you can invite him 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 problem: in the abperson reference, there is a constant to PostCodeExtension. In the case of the EPC, for example, there is the constant kABPersonAddressZIPKey. Perhaps this means that there is no formal support for this constant and, for example, it may cease to exist in future versions. Internally, the Geoservices framework contains a property called 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 have just forgotten about it. I suggest you open a radar requesting an equivalent constant and write your code bearing in mind that this key may be renamed or cease to exist.

  • Hello Friend! Perfect! It worked! Thank you very much for your help, I will follow your guidelines.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.