在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
就像我的手指快速移动然后标记看起来像在手指后面移动
答案 0 :(得分:1)
就我的经验而言,GM框架中没有简单的内置解决方案。不幸的是,GMSMarker
对象允许您跟踪(几乎没有延迟)的唯一交互式事件是一个简单的点击,它触发mapView的委托中相应的回调。如果您想要更复杂的东西,则必须在执行拖放操作时使用放置在地图上方的自定义标记视图来实现内容。这是算法:
1)将UIPanGestureRecognizer
添加到mapView。设置它的委托以解决与mapView的内置gestureRecognizers的冲突。
2)当您开始平移时,在gestureRecognizerShouldBegin
中决定用户是应用平移来映射还是标记。如果要标记,请允许panGR触发(return true
)。
3)隐藏您要移动的GMSMarker
对象(将不透明度设置为零或仅从地图中移除)
4)在手指位置下插入您的自定义独立 MarkerView (在步骤3中可视地复制已删除的一个)
5)使用panGR的更新移动此自定义视图。
6)在手指释放时,将自定义 MarkerView 替换为最终手指位置上的GMSMarkerView
,以便将其固定到地图上。