iDeveloper iDeveloper - 1 year ago 191
Swift Question

Get address by latitude/longitude in Swift using GoogleMap SDK

Hi Want to get Address on map, I am successfully get latitude and longitude, but am facing an issue in reverse geocoding to show address.

Please suggest a simple demo so that i can start work with that.

Answer Source

Retrieve your address like this :-

func getAddress(latitude: CLLocationDegrees, longitude: CLLocationDegrees, completionBlock : ((addrs : String)->Void)) {

 let url = NSURL(string: "\(baseUrl)latlng=\(latitude),\(longitude)&key=\(serverKey)")

        let number = String()
        let street = String()
        let city = String()
        let state = String()
        let zip = String()

  let data = NSData(contentsOfURL: url!)
  let json = try! NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments) as! NSDictionary
    if let result = json["results"] as? NSArray {
      if let address = result[0]["address_components"] as? NSArray {
        if let num = address[0]["short_name"] as? String{
           number = num
        if let str = address[1]["short_name"] as? String{
           street = str
        if let cit = address[2]["short_name"] as? String{
            city = cit
        if let st = address[4]["short_name"] as? String{
             state = st
        if let zp = address[6]["short_name"] as? String{
              zip = zp
        print("\(number) \(street), \(city), \(state) \(zip)")
        completionBlock(addrs: "\(number) \(street), \(city), \(state) \(zip)")



When you call this function it will look like this :-

getAddress(lat, longitude : long , {(addrs) in 

       print("the address of the asked coordinates is : \(addrs)")        


Look up my answer here : -