-1

I have longitude and latitude because I use didLongPressAtCoordinate and for now I want to assign marker.title to title of this coordinate.

func mapView(mapView: GMSMapView!, didLongPressAtCoordinate coordinate: CLLocationCoordinate2D) {

    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2D(latitude: coordinate.latitude, longitude: coordinate.longitude)
    self.googleMapsView.clear()
    self.googleMapsView.myLocationEnabled = false
    marker.map = self.googleMapsView
    marker.title = --> //my problem


}

i can't find good solution for that

pedrouan
  • 12,762
  • 3
  • 58
  • 74
scbas1000
  • 121
  • 1
  • 12
  • 4
    Hey scbas1000, welcome to Stack Overflow. Your question is very vague, and has little chance of being answered. Please visit out guide on [how to ask](http://stackoverflow.com/help/how-to-ask). – Alexander Aug 22 '16 at 14:49
  • thanks for your attention , i edit it , now is it right i think – scbas1000 Aug 22 '16 at 14:54
  • 1
    @scbas1000 no, your question is still very vague and you have not posted any code showing what you have so far. Post the code you have, clearly discussing what you have working at the minute, what you are trying to achieve (examples will help) and then list what the error / issue it is that you are facing in getting there – Simon McLoughlin Aug 22 '16 at 14:57
  • @scbas1000 what is `//my problem` supposed to mean? – Alexander Aug 22 '16 at 15:06
  • i can't find good solution just that – scbas1000 Aug 22 '16 at 15:06
  • you want to give title to this marker -> its address? – Dravidian Aug 22 '16 at 15:12
  • yes , with its longitude and latitude , thanks – scbas1000 Aug 22 '16 at 15:13
  • Is your input desired locations `address` somewhere, anywhere in your code or just these coordinates? – Dravidian Aug 22 '16 at 15:21
  • no , i touch somewhere in map and after i have longitude and latitude and i need its title (or address) – scbas1000 Aug 22 '16 at 15:23

2 Answers2

1

Get your address like this :-

  func getAddressForLatLng(latitude: CLLocationDegrees, longitude: CLLocationDegrees, completionBlock : ((addrs : String)->Void)) {
    let url = NSURL(string: "\(baseUrl)latlng=\(latitude),\(longitude)&key=\(serverKey)")
    print(url)


    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("\(number) \(street), \(city), \(state) \(zip)")
            completionBlock(addrs: "\(number) \(street), \(city), \(state) \(zip)")

        }
    }
}

Update your didLongPressAtCoordinate function :-

func mapView(mapView: GMSMapView!, didLongPressAtCoordinate coordinate: CLLocationCoordinate2D) {

    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2D(latitude: coordinate.latitude, longitude: coordinate.longitude)
    self.googleMapsView.clear()
    self.googleMapsView.myLocationEnabled = false
    marker.map = self.googleMapsView
    getAddressForLatLng(coordinate.latitude, longitude: coordinate.longitude) { (addrs) in

        marker.title = addrs
        marker.snippet = "\(coordinate.latitude),\(coordinate.longitude)"

    }


}

Mind that :- baseUrl And serverKey are your Google console baseUrl and serverKey

Dravidian
  • 9,945
  • 3
  • 34
  • 74
  • You do realize that state and city do not go to constant spots and that your code is wrong quite often. On top of that it is strictly written to work with a majority of the USA and wouldn't work the majority outside of it. – Sethmr Aug 23 '16 at 00:32
0

You need to geocode the coordinates. You have a limitation of 50 times you can do this per minute (set by google), so be efficient with use of doing so. Here is a tutorial on the process

Once you have the address, you just say

marker.title = "\(theAddressVariableName)\n\nLat: \(coordinate.latitude)\nLon: coordinate.longitude"

Format it however you want. Also, I don't understand why you would have the line of code self.googleMapsView.myLocationEnabled = false inside of that function. It should probably go somewhere else, but I will allow the benefit of the doubt on its placement.

Sethmr
  • 3,046
  • 1
  • 24
  • 42