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' –
是,我做了主線程,但標記不出現 –