UITapGestureRecogniser in GMSMapView

2.5k Views Asked by At

I am creating an app using swift. In one of my ViewController, I have a GMSMapView that I create programatically. I want user to have the capability triggering an action when clicking on the map.

What I have done :

import UIKit

class MapViewController: UIViewController, GMSMapViewDelegate {

let mapView = GMSMapView()

override func viewDidLoad() {
        super.viewDidLoad()

        mapView.delegate = self
        mapView.settings.scrollGestures = false
        mapView.frame = CGRectMake(0, 65, 375, 555)
        view.addSubview(mapView)


        var tap = UITapGestureRecognizer(target: self, action: "tap:")
        mapView.addGestureRecognizer(tap)
}

func tap(recogniser:UITapGestureRecognizer)->Void{
        println("it works")
    }

}

I have tried to override touchesBegan, didnt work. I have tried to insert mapView.userInteractionEnabled = true, didnt work...

Any idea?

3

There are 3 best solutions below

0
On

I managed to do it with

func mapView(mapView: GMSMapView!, didTapAtCoordinate coordinate: CLLocationCoordinate2D) {
        println("It works")
    }

But if someone could explain to me why the other solution didn't work, it would be great!

0
On

The map view already has its own gesture recognizers for panning, zooming etc.

So you probably need to tell the system that it should take care on multiple gesture recognizers.

As part of the UIGestureRecognizerDelegate protocol:

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}
1
On

You can use default MapVIew LongPress event

  /**

 * Called after a long-press gesture at a particular coordinate.
 *
 * @param mapView The map view that was pressed.
 * @param coordinate The location that was pressed.
 */
     - (void)mapView:(GMSMapView *)mapView
    didLongPressAtCoordinate:(CLLocationCoordinate2D)coordinate;