soling
soling

Reputation: 303

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?

Upvotes: 9

Views: 2587

Answers (3)

Samvel
Samvel

Reputation: 21

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;

Upvotes: 2

zisoft
zisoft

Reputation: 23078

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
}

Upvotes: 1

soling
soling

Reputation: 303

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!

Upvotes: 8

Related Questions