0

Using Google Maps iOS SDK, I want to be able to print the coordinates of a GMSMarker if long pressed on that particular marker.

I first get all of my coordinates from a dictionary and drop markers for all coordinates on the map:

func placeMapMarkers() {
    for item in self.finalDictionary as [Dictionary<String, String>] {
        let lat = item["lat"] as! String
        let lon = item["lon"] as! String
        let SpotLat = Double(lat)
        let SpotLon = Double(lon)
        let SpotLocation = CLLocationCoordinate2DMake(SpotLat!, SpotLon!)

        DispatchQueue.main.async(execute: {
            self.SpotMarker = GMSMarker(position: SpotLocation)
            self.SpotMarker?.icon = self.imageWithImage(image: UIImage(named: "SpotIcon")!, scaledToSize: CGSize(width: 35.0, height: 35.0))
            self.SpotMarker?.title = "Long press to navigate here"
            self.SpotMarker?.map = self.mapView
        })

        longPress(mapView: self.mapView, didLongPressAtCoordinate: SpotLocation)
    }
}

My longPress function call is in the above placeMapMarkers function itself, since I want to identify while placing markers itself if a particular marker has been long pressed (I could be wrong with my thinking here).

My longPress function is below.

//This is long Press function:-
func longPressView(mapView: GMSMapView!, didLongPressAtCoordinate coordinate: CLLocationCoordinate2D) {
    //Here handle your long press on map marker like:-
    print("long pressed at \(coordinate)")
}

The problem is that I am getting "long pressed at" all coordinates from the dictionary of coordinates.

I want to

  1. place markers for all coordinates in a dictionary
  2. long press on a particular marker
  3. and print coordinates for only that particular marker which was long pressed.

How do I go about this? Had a look at the other solutions, wasn't able to work out much.

Kanav Batra
  • 35
  • 1
  • 10

2 Answers2

1

I looked through the GMSMapView API. There is a method called "didLongPressAtCoordinate" that passes in a CLLocationCoordinate2D, so I think you can use that to create a GMSMarker. See here

You have to implement the GMSMapViewDelegate, and you can call

func mapView(mapView: GMSMapView!, didLongPressAtCoordinate coordinate: CLLocationCoordinate2D) {
    let marker = GMSMarker(position: coordinate)
    marker.title = "Found You!"
    marker.map = mapView
}

Hope this one helps :)

Ryan Jin
  • 119
  • 1
  • 8
0

I have done this before. You basically have to convert the touch point on the map to a coordinate.

@IBOutlet weak var mapView: MKMapView!

override func viewDidLoad() {
    let uilpgr = UILongPressGestureRecognizer(target: self, action: #selector(userPerformedLongPress(gesture:)))
    uilpgr.minimumPressDuration = 2.0
}

func userPerformedLongPress(gesture: UIGestureRecognizer) {
    let touchPoint = gesture.location(in: mapView)
    let newCoordinate: CLLocationCoordinate2D = mapView.convert(touchPoint, toCoordinateFrom: mapView)
    let annotation = MKPointAnnotation()
    annotation.coordinate = newCoordinate
    annotation.title = "Location Selected"
    annotation.subtitle = "Coordinate: \(round(1000*newCoordinate.longitude)/1000), \(round(1000*newCoordinate.latitude)/1000)"
    mapView.addAnnotation(annotation)
    print("Gesture recognized")
}
Ryan Jin
  • 119
  • 1
  • 8