Good morning SO!
I am currently working on an iOS app that requires me to use Google Maps' reverse geocoding features. The tricky part is that I need to be able to access the full list of address components (i.e. the full "address_components" value from the JSON returned from this example, including short and long names)
When using the Google Maps iOS SDK, it seems that all results from reverse geocoding are given in the form of GMSAddress
, which only exposes a small subset of these values (and no short versions). I have so far not found a way to access what I need.
Is there any way to get these values through the iOS SDK? If not, is it okay to try and call the google maps API directly? Has anybody tried?
Thank you for your time,
Julien P.
I found an easy solution using the LMGeocoder library for iOS.
Inside LMAddress
, there is a lines
attribute that will contain all the information. You can access it easily with the following piece of code.
func getAddressComponent(name: String, lines: [[NSObject: AnyObject?]]) -> (short: String?, long: String?)? {
let filteredLines = lines.filter { ($0["types"] as! [String]).contains(name)
guard let line = filteredLines.first else {
return nil
}
let longName = line["long_name"] as? String
let shortName = line["short_name"] as? String
return (short: shortName, long: longName)
}
This is using Swift 2.3 but will probably work for Swift 3 as well. Hope it helps anyone! :)
Julien