Search code examples
iosgoogle-maps-sdk-iosreverse-geocoding

How to obtain country, state, city from reverseGeocodeCoordinate?


GMSReverseGeocodeResponse contains

- (GMSReverseGeocodeResult *)firstResult;

whose definition is like:

@interface GMSReverseGeocodeResult : NSObject<NSCopying>

/** Returns the first line of the address. */
- (NSString *)addressLine1;

/** Returns the second line of the address. */
- (NSString *)addressLine2;

@end

Is there any way to obtain the country, ISO country code, state (administrative_area_1 or corresponding one) from those two strings (valid for all the countries and all the addresses)?

NOTE: I tried to execute this piece of code

[[GMSGeocoder geocoder] reverseGeocodeCoordinate:CLLocationCoordinate2DMake(40.4375, -3.6818) completionHandler:^(GMSReverseGeocodeResponse *resp, NSError *error)
 {
    NSLog( @"Error is %@", error) ;
    NSLog( @"%@" , resp.firstResult.addressLine1 ) ;
    NSLog( @"%@" , resp.firstResult.addressLine2 ) ;
 } ] ;

But for some reason the handler was never called. I did add the app key, and also added the iOS bundle id to the app key. No error is printed in the console. With this I mean I am not aware of the content of the lines.


Solution

  • The simplest way is to upgrade to Version 1.7 of the Google Maps SDK for iOS (released February 2014).
    From the release notes:

    GMSGeocoder now provides structured addresses via GMSAddress, deprecating GMSReverseGeocodeResult.

    From GMSAddress Class Reference, you can find these properties:

    coordinate
    Location, or kLocationCoordinate2DInvalid if unknown.

    thoroughfare
    Street number and name.

    locality
    Locality or city.

    subLocality
    Subdivision of locality, district or park.

    administrativeArea
    Region/State/Administrative area.

    postalCode
    Postal/Zip code.

    country
    The country name.

    lines
    An array of NSString containing formatted lines of the address.

    No ISO country code though.
    Also note that some properties may return nil.

    Here's a full example:

    [[GMSGeocoder geocoder] reverseGeocodeCoordinate:CLLocationCoordinate2DMake(40.4375, -3.6818) completionHandler:^(GMSReverseGeocodeResponse* response, NSError* error) {
        NSLog(@"reverse geocoding results:");
        for(GMSAddress* addressObj in [response results])
        {
            NSLog(@"coordinate.latitude=%f", addressObj.coordinate.latitude);
            NSLog(@"coordinate.longitude=%f", addressObj.coordinate.longitude);
            NSLog(@"thoroughfare=%@", addressObj.thoroughfare);
            NSLog(@"locality=%@", addressObj.locality);
            NSLog(@"subLocality=%@", addressObj.subLocality);
            NSLog(@"administrativeArea=%@", addressObj.administrativeArea);
            NSLog(@"postalCode=%@", addressObj.postalCode);
            NSLog(@"country=%@", addressObj.country);
            NSLog(@"lines=%@", addressObj.lines);
        }
    }];
    

    and its output:

    coordinate.latitude=40.437500
    coordinate.longitude=-3.681800
    thoroughfare=(null)
    locality=(null)
    subLocality=(null)
    administrativeArea=Community of Madrid
    postalCode=(null)
    country=Spain
    lines=(
        "",
        "Community of Madrid, Spain"
    )
    

    Alternatively, you may consider using Reverse Geocoding in the The Google Geocoding API (example).