0

I have a latitude and longitude location of marker in google maps that I want to convert to the location name String in Swift. What is the best way to do this? i want to show markers' location address and i don't know how to do it .

here is my code that i used to add marker and get latitude and longitude:

func mapView(mapView: GMSMapView, didLongPressAtCoordinate coordinate: CLLocationCoordinate2D) {
        if counterMarker < 2
        {
            counterMarker += 1
            let marker = GMSMarker(position: coordinate)
            marker.appearAnimation = kGMSMarkerAnimationPop

              marker.map = mapView
            marker.position.latitude = coordinate.latitude
            marker.position.longitude = coordinate.longitude

            print(marker.position.latitude)
            print(marker.position.longitude)


            }
    }
P.Tb
  • 99
  • 1
  • 5
  • 15

1 Answers1

3
    func mapView(mapView: GMSMapView, didLongPressAtCoordinate coordinate: CLLocationCoordinate2D) {
            if counterMarker < 2
            {
                counterMarker += 1
                let marker = GMSMarker(position: coordinate)
                marker.appearAnimation = kGMSMarkerAnimationPop

                  marker.map = mapView
                marker.position.latitude = coordinate.latitude
                marker.position.longitude = coordinate.longitude

                   self.getAddressForLatLng(String(format: "%@",marker.position.latitude), longitude:String(format: "%@",marker.position.longitude)

                }
        }


  func getAddressForLatLng(latitude: String, longitude: String) {
        let url = NSURL(string: "https://maps.googleapis.com/maps/api/geocode/json?latlng=\(latitude),\(longitude)&key=YOUR-APIKEY")
        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 {
                let number = address[0]["short_name"] as! String
                let street = address[1]["short_name"] as! String
                let city = address[2]["short_name"] as! String
                let state = address[4]["short_name"] as! String
                let zip = address[6]["short_name"] as! String
                print("\n\(number) \(street), \(city), \(state) \(zip) \(address)")
            }
        }
    }
Hardik Thakkar
  • 15,269
  • 2
  • 94
  • 81
  • 1
    i have this error : 'sendSynchronousRequest(_:returningResponse:)' was deprecated in iOS 9.0: Use [NSURLSession dataTaskWithRequest:completionHandler:] (see NSURLSession.h!! and i don't see any address in my app – P.Tb Jun 14 '16 at 06:46
  • now it can't make any marker any more ! and error is for this line: self.getAddressForLatLng(String(format: "%@",marker.position.latitude), longitude:String(format: "%@",marker.position.longitude)) – P.Tb Jun 14 '16 at 07:23
  • when i tap on screen to make marker i see this thread : Thread1: EXC_BAD_ACCESS(code=1,address=0x4041e097014cefe) – P.Tb Jun 14 '16 at 07:39