Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
453 views
in Technique[技术] by (71.8m points)

ios - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...