【问题标题】:swift 4 Google Map - how to update marker position using gesture.positionswift 4 Google Map - 如何使用gesture.position更新标记位置
【发布时间】:2018-10-13 15:45:49
【问题描述】:

viewDidLoad 中,我将gesturerecognizers 添加到mapView,并将consumeGestureInView 设置为true 在 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 就像我的手指快速移动然后标记看起来像在手指后面移动

【问题讨论】:

  • 我想像 panGestureRecognizer 那样移动标记

标签: ios swift google-maps google-maps-markers swift4


【解决方案1】:

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

1) 将UIPanGestureRecognizer 添加到mapView。设置它的委托来解决与 mapView 的内置手势识别器的冲突。

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

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

4) 在手指位置下插入您自定义的独立 MarkerView(在视觉上复制第 3 步中删除的)

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

6) 在手指释放时,将您的自定义 MarkerView 替换为最终手指位置上的 GMSMarkerView,以便将其固定到地图背面。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-05
    • 1970-01-01
    • 1970-01-01
    • 2012-01-28
    • 2014-03-25
    • 2019-03-04
    • 1970-01-01
    相关资源
    最近更新 更多