通过在地图上滑动来拖动 GoogleMaps-Marker

Drag GoogleMaps-Marker via swiping over map

我有一个 Google-地图 + 标记。我知道如何使标记可拖动。标准行为是 'Long-Press' 一个标记,您可以拖动它。 我想要的是通过在地图上滑动来拖动标记。不必击中标记。用户在地图上从左向右滑动,同时标记从左向右改变位置,距离等于滑动长度。

我在GM-API中找不到合适的解决方案。有什么想法吗?

我正在使用 Swift 2.2

var marker: GMSMarker!   

func createMarker(title: String, target: CLLocationCoordinate2D) {
    marker = GMSMarker(position: target)
    marker.appearAnimation = kGMSMarkerAnimationPop
    marker.map = map
}

func activateDragMode() {
    marker.draggable = true
    map.settings.scrollGestures = false
    map.settings.zoomGestures = false
    map.settings.rotateGestures = false
}

GoogleMap-API没有提供我需要的方法。但我找到了解决方案:

map.settings.consumesGesturesInView = false
let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(panRecognition))
view.addGestureRecognizer(panGestureRecognizer)

func panRecognition(recognizer: UIPanGestureRecognizer) {
    if marker.draggable {
        let markerPosition = map.projection.pointForCoordinate(marker.position)
        let translation = recognizer.translationInView(view)
        recognizer.setTranslation(CGPointZero, inView: view)
        let newPosition = CGPointMake(markerPosition.x + translation.x, markerPosition.y + translation.y)
        marker.position = map.projection.coordinateForPoint(newPosition)
    }
}

我必须停用 'consumesGesturesInView' 才能添加我自己的 PanGestureRecognizer,它可以操纵标记。

Swift 5.1

通过google api等方法分析,没有得到合适的。这个问题的最佳答案是使用平移手势。

将平移手势添加到 mapView 为

self.mapView.settings.consumesGesturesInView = false
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(self. panHandler(_:)))
self.mapView.addGestureRecognizer(panGesture)

平移手势方法的实现如

@objc private func panHandler(_ pan : UIPanGestureRecognizer){

        if pan.state == .ended{
            let mapSize = self.mapView.frame.size
            let point = CGPoint(x: mapSize.width/2, y: mapSize.height/2)
            let newCoordinate = self.mapView.projection.coordinate(for: point)
            print(newCoordinate)
             //do task here
        }
}