view raw
DanKas DanKas - 9 months ago 98
Swift Question

Drag GoogleMaps-Marker via swiping over map

I have a Google-Map + marker. I know how to make a marker draggable. The standard behaviour is to 'Long-Press' a marker and you can drag it.
What I want is to drag the marker by swiping over the map. It shouldn't be neccessary to hit the marker. User swipes over the map from left to right and simultanously the marker changes position from left to right where the distance equals swipe-length.

I can't find a suitable solution in the GM-API. Any ideas?

I'm using Swift 2.2

var marker: GMSMarker!

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

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


The GoogleMap-API doesn't provide the method I need. But i found a solution:

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

func panRecognition(recognizer: UIPanGestureRecognizer) {
    if marker.draggable {
        let markerPosition = CGPointMake(CGFloat(marker.position.longitude), CGFloat(marker.position.latitude))
        let translation = recognizer.translationInView(view)
        let newPosition = CGPointMake(markerPosition.x + translation.x +, markerPosition.y + translation.y +
        marker.position = map.projection.coordinateForPoint(newPosition)

I had to deactivate 'consumesGesturesInView' to add my own PanGestureRecognizer, which manipulates the marker.