0
我想用UILongPressGestureRecognizer添加标记,但是我的代码不起作用,我做错了什么?用UILongPressGestureRecognizer添加标记到GoogleMaps swift3
func setupGesture() {
let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(action))
longPressRecognizer.minimumPressDuration = 1.0
mapView.addGestureRecognizer(longPressRecognizer)
}
func action(recognizer: UILongPressGestureRecognizer) {
if (recognizer.state == .ended) {
DispatchQueue.main.async {
let longPressPoint = recognizer.location(in: mapView)
let coordinate = self.mapView.projection.coordinate(for: longPressPoint)
let marker = GMSMarker(position: coordinate)
marker.title = "Hello World"
marker.map = mapView
}
}
}
此方法不工作太..
extension MapViewController: GMSMapViewDelegate {
func mapView(_ mapView: GMSMapView, didLongPressAt coordinate: CLLocationCoordinate2D) {
DispatchQueue.main.async {
let marker = GMSMarker(position: coordinate)
marker.title = "Hello World"
marker.map = mapView
}
}
}
确保你在主线程中更新UI'DispatchQueue.main.async {//你的标记code' –
是,我做了主线程,但标记不出现 –