UITapGestureRecogniser in GMSMapView
Asked Answered
S

3

9

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?

Simmer answered 19/11, 2014 at 12:36 Comment(1)
I added a line of code to your code mapView.isUserInteractionEnabled = true But still not working.Anthropophagite
S
8

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!

Simmer answered 19/11, 2014 at 12:56 Comment(0)
T
2

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;
Tinderbox answered 27/7, 2017 at 16:23 Comment(1)
Can you edit out the bullet points and make proper code blocks? This isn't really possible to read.Butz
P
1

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
}
Pour answered 19/11, 2014 at 13:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.