Using GMSAddress and GMSGeocoder to return Coordinates from Address in Swift
Asked Answered
G

1

6

Is there a way to get the Coordinates from an entered Address string using GMS in iOS Swift? I can find examples of returning an Address from Coordinates (reverse geocoding?), but not the other way around. Does google offer this service? To first parse the entered address string, return the most suitable actual address, and ultimately the coordinates. Please provide a simple example, or point me in the right direction. Regards, Chris

Grandmamma answered 17/2, 2015 at 8:40 Comment(0)
H
7

As of June 2015, GMS iOS SDK does not expose this functionality directly. However, there are two ways to get it. First one is using Google Maps Web API.

let baseURLGeocode = "https://maps.googleapis.com/maps/api/geocode/json?"

func geocodeAddress(address: String!, withCompletionHandler completionHandler: ((status: String, success: Bool) -> Void)) {

    if let lookupAddress = address {

        var geocodeURLString = baseURLGeocode + "address=" + lookupAddress
        geocodeURLString = geocodeURLString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!

        let geocodeURL = NSURL(string: geocodeURLString)

        dispatch_async(dispatch_get_main_queue(), { () -> Void in
            let geocodingResultsData = NSData(contentsOfURL: geocodeURL!)

            var error: NSError?
            let dictionary: Dictionary<NSObject, AnyObject> = NSJSONSerialization.JSONObjectWithData(geocodingResultsData!, options: NSJSONReadingOptions.MutableContainers, error: &error) as Dictionary<NSObject, AnyObject>

            if (error != nil) {
                println(error)
                completionHandler(status: "", success: false)
            }
            else {
                // Get the response status.
                let status = dictionary["status"] as String

                if status == "OK" {
                    let allResults = dictionary["results"] as Array<Dictionary<NSObject, AnyObject>>
                    self.lookupAddressResults = allResults[0]

                    // Keep the most important values.
                    self.fetchedFormattedAddress = self.lookupAddressResults["formatted_address"] as String
                    let geometry = self.lookupAddressResults["geometry"] as Dictionary<NSObject, AnyObject>
                    self.fetchedAddressLongitude = ((geometry["location"] as Dictionary<NSObject, AnyObject>)["lng"] as NSNumber).doubleValue
                    self.fetchedAddressLatitude = ((geometry["location"] as Dictionary<NSObject, AnyObject>)["lat"] as NSNumber).doubleValue

                    completionHandler(status: status, success: true)
                }
                else {
                    completionHandler(status: status, success: false)
                }
            }
        })
    }
    else {
        completionHandler(status: "No valid address.", success: false)
    }
}

You can find the complete description of this method here: http://www.appcoda.com/google-maps-api-tutorial/

Second way is to use apple native CoreLocation framework

func geoLocate(address:String!){
let gc:CLGeocoder = CLGeocoder()

gc.geocodeAddressString(address, completionHandler: { (placemarks: [AnyObject]!, error: NSError!) -> Void in
    let pm = placemarks as! [CLPlacemark]
    if (placemarks.count > 0){
        let p = pm[0]

        var myLatitude = p.location.coordinate.latitude
        var myLongtitude = p.location.coordinate.longitude

        //do your stuff
    }
})
}

EDIT: Swift 4.2

func geoLocate(address:String!){
        let gc:CLGeocoder = CLGeocoder()
        gc.geocodeAddressString(address) { (placemarks, error) in
            if ((placemarks?.count)! > 0){
                let p = placemarks![0]
                print("Lat: \(p.location?.coordinate.latitude) Lon: \(p.location?.coordinate.longitude)")
            }

    }
} 
Howdoyoudo answered 14/6, 2015 at 16:56 Comment(1)
is "maps.googleapis.com/maps/api/geocode/json?" api paid or unpaid ?Dimmer

© 2022 - 2024 — McMap. All rights reserved.