【问题标题】:Drag GoogleMaps-Marker via swiping over map通过在地图上滑动来拖动 GoogleMaps-Marker
【发布时间】:2016-07-22 12:39:33
【问题描述】:

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

我在 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
}

【问题讨论】:

标签: swift google-maps google-maps-sdk-ios


【解决方案1】:

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,它会操纵标记。

【讨论】:

  • 但是 view.addGestureRecognizer(panGestureRecognizer) 中的“视图”是什么?
【解决方案2】:

斯威夫特 5.1

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

在mapView中添加平移手势

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

平移手势方法as的实现

@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
        }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-20
    • 2015-09-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多