我注意到:
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”从不打印,但“You tapped at so and so”会打印。
我的 map View 被拉伸(stretch)以填满 View 屏幕。似乎 didTapAtCoordinate
覆盖了 GestureRecognizer。 (我确信这一点,因为当我将 gesturerecognizer 附加到该 Controller 的 super View (最顶层 View ),并在 mapView 上禁用“用户交互”时,“Tapped”被打印出来。)如何调用这两种方法?
最佳答案
这是一个 worker 类(Class)来比较你做错了什么:
可能是:
没有调用 GMSMapViewDelegate
MapView.delegate = self 未设置
缺少
前面的下划线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 } }
关于swift - 将谷歌地图的 didTapAtCoordinate 方法与 UITapGestureRecognizer 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34535923/