Tom Derry Marion Mantilla Tom Derry Marion Mantilla - 23 days ago 7
Swift Question

How to convert Latitude and Longitude into an address in GoogleMaps in Swift?

I've been searching for this but couldn't find an answer, I wish to convert my latitude and longitude into a plain text address. Because I want to add the address in the marker snippet.
Thanks in advance.

[EDIT]
Here is my code

var Addressxx: String = ""

let reverseGeoCoder = GMSGeocoder()

reverseGeoCoder.reverseGeocodeCoordinate(coor, completionHandler: {(placeMark, error) -> Void in

if error == nil {

if let placeMarkObject = placeMark {

if placeMarkObject.results()?.count > 0 {

Addressxx = (placeMarkObject.firstResult()?.lines)! // Error can't assign [String] to 'String'
} else {
//Do Nothing
}
} else {
//Do Nothing
}
} else {
print(error?.localizedDescription)
}
})

let time :String = NSLocalizedString("Tracked on: ", comment:"Tracked on: ") + dfmatter.stringFromDate(date)

let unit = self.speed_unit

let typeStr = dicz.objectForKey("type") as! String

if (dicz.allKeys[2] as! String == "battery" ){

let speed:String = NSLocalizedString("Speed: ", comment:"Speed: ") + (dicz.objectForKey("speed")?.stringValue)! + "/" + unit

let battery :String = NSLocalizedString("battery: ", comment:"battery: ") + (dicz.objectForKey("battery")?.stringValue)! + "%"

marker.snippet = battery + "\n" + time + speed + "(" + typeStr + ")"


} else {

let typeStr = dicz.objectForKey("type") as! String

let speed:String = NSLocalizedString("Speed: ", comment:"Speed: ") + (dicz.objectForKey("speed")?.stringValue)! + "/" + unit + typeStr

marker.snippet = Addressxx + "\n" + time + "\n" + speed + "\n" + "Type: (" + typeStr + ")"

}

Answer

You can use reverse geocoding to get the address from coordinate like this,

let reverseGeoCoder = GMSGeocoder()
let coordinate = "You coordinate will goes here"
    reverseGeoCoder.reverseGeocodeCoordinate(coordinate, completionHandler: {(placeMark, error) -> Void in
        if error == nil {
            if let placeMarkObject = placeMark {
               if placeMarkObject.results()?.count > 0 {
                 self.tappingAddress = (placeMarkObject.firstResult()?.lines)! // You can get address here
               } else {
                 //Do Nothing
               }
            } else {
              //Do Nothing
            }
        } else {
            print(error?.localizedDescription)
        }
    })

Your modified code:

let reverseGeoCoder = GMSGeocoder()

    reverseGeoCoder.reverseGeocodeCoordinate(coor, completionHandler: {(placeMark, error) -> Void in

        if error == nil {

            if let placeMarkObject = placeMark {

                if placeMarkObject.results()?.count > 0 {

                    self.addressArray = (placeMarkObject.firstResult()?.lines)! // Error can't assign [String] to 'String'
                } else {
                    //Do Nothing
                }
            } else {
                //Do Nothing
            }
        } else {
            print(error?.localizedDescription)
        }
    })

    let time :String = NSLocalizedString("Tracked on: ", comment:"Tracked on: ") + dfmatter.stringFromDate(date)

    let unit = self.speed_unit

    let typeStr = dicz.objectForKey("type") as! String

    if (dicz.allKeys[2] as! String == "battery" ){

        let speed:String = NSLocalizedString("Speed: ", comment:"Speed: ") + (dicz.objectForKey("speed")?.stringValue)! + "/" + unit

        let battery :String = NSLocalizedString("battery: ", comment:"battery: ") + (dicz.objectForKey("battery")?.stringValue)! + "%"

        marker.snippet = battery + "\n" + time + speed + "(" + typeStr + ")"


    } else {

        let typeStr = dicz.objectForKey("type") as! String

        let speed:String = NSLocalizedString("Speed: ", comment:"Speed: ") + (dicz.objectForKey("speed")?.stringValue)! + "/" + unit + typeStr

        for i in addressArray {
            Addressxx = Addressxx + i + " "
        }

        marker.snippet =  Addressxx + "\n" + time + "\n" + speed + "\n" + "Type: (" + typeStr + ")"

    }

Thanks:)