Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITapGestureRecogniser in GMSMapView

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?

like image 268
soling Avatar asked Nov 19 '14 12:11

soling


3 Answers

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!

like image 108
soling Avatar answered Oct 31 '22 18:10

soling


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;
like image 34
Samvel Avatar answered Oct 31 '22 18:10

Samvel


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
}
like image 1
zisoft Avatar answered Oct 31 '22 18:10

zisoft