将google maps的didTapAtCoordinate方法与UITapGestureRecognizer结合使用

时间:2015-12-30 19:27:34

标签: swift uigesturerecognizer

我注意到了:

func mapView(mapView: GMSMapView!, didTapAtCoordinate coordinate: CLLocationCoordinate2D) {
    print("You tapped at \(coordinate.latitude), \(coordinate.longitude)")
}

正在覆盖手势识别器:

@IBOutlet weak var mapView: GMSMapView!
let aSelector : Selector = "tappedOnMap"
let tapGesture = UITapGestureRecognizer(target: self, action: aSelector)
mapView.userInteractionEnabled = true
tapGesture.numberOfTapsRequired = 1
mapView.addGestureRecognizer(tapGesture)

func tappedOnMap() {
    print("Tapped")
}

“Tapped”永远不会打印,但是“你这么做”就是这样。

我的地图视图已展开以填充视图屏幕。似乎didTapAtCoordinate覆盖了GestureRecognizer。 (我确信这是因为当我将手势识别器附加到该控制器的超级视图(最顶层视图)并在mapView上禁用“用户交互”时,“Tapped”会被打印。)如何调用这两种方法?

1 个答案:

答案 0 :(得分:0)

这是一个工作班,用来比较您做错了什么:

可能是:

  1. 未调用GMSMapViewDelegate

  2. MapView.delegate =未设置自我

  3. func mapView(**_** MapView: GMSMapView, didTap marker: GMSMarker) -> Bool

    前缺少下划线
    class CustomMapPicker: UIViewController, GMSMapViewDelegate
    {
        @IBOutlet weak var MapView: GMSMapView!
    
    override func viewDidLoad()
    {
        super.viewDidLoad()
        MapView.delegate = self
        let camera = GMSCameraPosition.camera(withLatitude: 25.2048, longitude: 55.2708, zoom: 10)
        MapView?.camera = camera
        MapView?.animate(to: camera)
        MapView?.isMyLocationEnabled = false
    }
    
    func mapView(_ MapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D)
    {
        print("lat = " + "\(coordinate.latitude)" + " long = " +  "\(coordinate.longitude)")
    }
    
    func mapView(_ MapView: GMSMapView, didTap marker: GMSMarker) -> Bool
    {
        return true
    }
    }