How to obtain country, state, city from reverseGeocodeCoordinate? How to obtain country, state, city from reverseGeocodeCoordinate? ios ios

How to obtain country, state, city from reverseGeocodeCoordinate?

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:

Location, or kLocationCoordinate2DInvalid if unknown.

Street number and name.

Locality or city.

Subdivision of locality, district or park.

Region/State/Administrative area.

Postal/Zip code.

The country name.

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=%@",;        NSLog(@"lines=%@", addressObj.lines);    }}];

and its output:

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

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

Answer in Swift

Using Google Maps iOS SDK (currently using the V1.9.2 you cannot specify the language in which to return results):

@IBAction func googleMapsiOSSDKReverseGeocoding(sender: UIButton) {    let aGMSGeocoder: GMSGeocoder = GMSGeocoder()    aGMSGeocoder.reverseGeocodeCoordinate(CLLocationCoordinate2DMake(self.latitude, self.longitude)) {        (let gmsReverseGeocodeResponse: GMSReverseGeocodeResponse!, let error: NSError!) -> Void in        let gmsAddress: GMSAddress = gmsReverseGeocodeResponse.firstResult()        print("\ncoordinate.latitude=\(gmsAddress.coordinate.latitude)")        print("coordinate.longitude=\(gmsAddress.coordinate.longitude)")        print("thoroughfare=\(gmsAddress.thoroughfare)")        print("locality=\(gmsAddress.locality)")        print("subLocality=\(gmsAddress.subLocality)")        print("administrativeArea=\(gmsAddress.administrativeArea)")        print("postalCode=\(gmsAddress.postalCode)")        print("country=\(")        print("lines=\(gmsAddress.lines)")    }}

Using Google Reverse Geocoding API V3 (currently you can specify the language in which to return results):

@IBAction func googleMapsWebServiceGeocodingAPI(sender: UIButton) {    self.callGoogleReverseGeocodingWebservice(self.currentUserLocation())}// #1 - Get the current user's location (latitude, longitude).private func currentUserLocation() -> CLLocationCoordinate2D {    // returns current user's location. }// #2 - Call Google Reverse Geocoding Web Service using AFNetworking.private func callGoogleReverseGeocodingWebservice(let userLocation: CLLocationCoordinate2D) {    let url = "\(userLocation.latitude),\(userLocation.longitude)&key=\(self.googleMapsiOSAPIKey)&language=\(self.googleReverseGeocodingWebserviceOutputLanguageCode)&result_type=country"    AFHTTPRequestOperationManager().GET(        url,        parameters: nil,        success: { (operation: AFHTTPRequestOperation!, responseObject: AnyObject!) in            println("GET user's country request succeeded !!!\n")            // The goal here was only for me to get the user's iso country code +             // the user's Country in english language.            if let responseObject: AnyObject = responseObject {                println("responseObject:\n\n\(responseObject)\n\n")                let rootDictionary = responseObject as! NSDictionary                if let results = rootDictionary["results"] as? NSArray {                    if let firstResult = results[0] as? NSDictionary {                        if let addressComponents = firstResult["address_components"] as? NSArray {                            if let firstAddressComponent = addressComponents[0] as? NSDictionary {                                if let longName = firstAddressComponent["long_name"] as? String {                                    println("long_name: \(longName)")                                }                                if let shortName = firstAddressComponent["short_name"] as? String {                                    println("short_name: \(shortName)")                                }                            }                        }                    }                }            }        },        failure: { (operation: AFHTTPRequestOperation!, error: NSError!) in            println("Error GET user's country request: \(error.localizedDescription)\n")            println("Error GET user's country request: \(operation.responseString)\n")        }    )}

I hope this code snippet and explanation will help future readers.

Swift 5 version for addresses in the United States:

import Foundationimport GoogleMapsextension GMSAddress {    var formattedAddress: String {        let addressComponents = [            thoroughfare,        // One Infinite Loop            locality,            // Cupertino            administrativeArea,  // California            postalCode           // 95014        ]        return addressComponents            .compactMap { $0 }            .joined(separator: ", ")    }}