swift 4 Google 地图 - 如何使用 gesture.position 更新标记位置

swift 4 Google Map - how to update marker position using gesture.position

viewDidLoad 中,我将 gesturerecognizers 添加到 mapView 并且还设置了 consumeGestureInView 在 handleTap 方法中,我将触摸点转换为 latLng,然后使用 latLng 设置标记位置,但标记移动速度非常慢

self.mapView.settings.consumesGesturesInView = true
for gestureRecognizer in self.mapView.gestureRecognizers! {
                                gestureRecognizer.addTarget(self, action: #selector(MapViewController.handleTap(_:)))
}

      @objc func handleTap(_ sender: UITapGestureRecognizer) {
                var allMarkers = markers
                if(sender.numberOfTouches == 1){
                    var positions = CGPoint()
                    var newPosition = CLLocationCoordinate2D()
                    let currentZoom = self.mapView.camera.zoom

                    switch (sender.state){
                    case .began:
                        positions = sender.location(in: self.mapView)
                        newPosition = self.mapView.projection.coordinate(for: positions)
                        let ind = self.getNearbymarkers(position: newPosition,markers:allMarkers)
                        allMarkers[ind].position = newPosition
                        self.mapView.settings.scrollGestures = false
                        mapView(self.mapView, didBeginDragging: allMarkers[ind])
                        self.mapView.settings.scrollGestures = true
                        break
                    case .ended:
                        positions = sender.location(in: self.mapView)
                        newPosition = self.mapView.projection.coordinate(for: positions)
                        let ind = self.getNearbymarkers(position: newPosition,markers:allMarkers)
                        print(ind)
                        allMarkers[ind].position = newPosition
                        self.mapView.settings.scrollGestures = false
                        mapView(self.mapView, didEndDragging: allMarkers[ind])
                        self.mapView.settings.scrollGestures = true
                        break
                    case .changed:
                        positions = sender.location(in: self.mapView)
                        newPosition = self.mapView.projection.coordinate(for: positions)
                        let ind = self.getNearbymarkers(position: newPosition,markers:allMarkers)
                        print(ind)
                        allMarkers[ind].position = newPosition
                        self.mapView.settings.scrollGestures = false
                        mapView(self.mapView, didDrag: allMarkers[ind])
                        self.mapView.settings.scrollGestures = true
                        break
                    default:
                        break
                    }
            }
        }

问题是当此行执行时标记呈现缓慢 allMarkers[ind].position = newPosition 就像我的手指快速移动然后标记看起来像在手指后面移动

根据我的经验,在 GM 框架中没有简单的内置解决方案。不幸的是,GMSMarker 个对象允许您跟踪(几乎没有延迟)的唯一交互事件是一个简单的点击,它会在 mapView 的委托中触发相应的回调。如果您想要更复杂的东西,则必须在执行拖放操作时使用放置在地图上方的自己的自定义标记视图来实现。这是算法:

1) 添加一个UIPanGestureRecognizer到mapView。设置它的委托来解决与 mapView 的内置 gestureRecognizers 的冲突。

2) 当您开始平移时,在 gestureRecognizerShouldBegin 中决定用户是将平移应用于地图还是标记。如果要标记,请允许 panGR 发射 (return true)。

3) 隐藏要移动的 GMSMarker 对象(将不透明度设置为零或直接从地图中删除)

4) 在手指位置

下插入自定义的独立 MarkerView(在视觉上复制步骤 3 中删除的那个)

5) 使用 panGR 的更新移动此自定义视图。

6) 手指松开时,将您的自定义 MarkerView 替换为最终手指位置上的 GMSMarkerView,以便返回地图。