Reputation: 293
In viewDidLoad
i added gesturerecognizers
to mapView
and also set true the consumeGestureInView
and in the handleTap Method I convert the touch point into latLng and then set the marker position with latLng but the marker move slow very slow
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
}
}
}
The issue is when this line execute then the marker rendered slow allMarkers[ind].position = newPosition
like my finger move fast then the marker looks like moving behind the finger
Upvotes: 2
Views: 1975
Reputation: 1217
As for my experience, there are no easy built-in solutions in GM framework for that. Unfortunately, the only interactive event that GMSMarker
objects allow you to track (almost with no delays) is a simple tap, which triggers a corresponding callback in mapView's delegate. If you want something more complex, you have to implement stuff with your own custom marker view placed above map while performing drag&drop operation. Here is the algorithm:
1) Add a UIPanGestureRecognizer
to the mapView. Set it's delegate to resolve conflicts with mapView's built-in gestureRecognizers.
2) When you start panning, in gestureRecognizerShouldBegin
decide whether the user apply pan to map or to a marker. If to marker, allow panGR to fire (return true
).
3) Hide GMSMarker
object which you wish to move (set opacity to zero or just remove from map)
4) Insert your custom independent MarkerView (which visually duplicates the removed one at step 3) under the finger position
5) Move this custom view with the panGR's updates.
6) On finger release, replace your custom MarkerView with the GMSMarkerView
on the final finger location in order to pin in to the map back.
Upvotes: 1