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 + ")"

}

Upvotes: 1

Views: 850

Answers (1)

Karthick Selvaraj
Karthick Selvaraj

Reputation: 2505

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

Upvotes: 1

Related Questions