我有一个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
}
答案 0 :(得分:2)
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,它操纵标记。
答案 1 :(得分:0)
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
}
}