Search code examples
iosgoogle-maps-sdk-ios

iOS Google Maps Marker drag event


I am building an iOS application using google maps SDK. I can add some markers on the maps when user does a longPressAtCoordinate. My problem is that when I am trying to drag a marker the diiLongPressAtCoordinate is fired before the didBeginDraggingMarker so a new marker is added also.

-(void)mapView:(GMSMapView *)mapView didBeginDraggingMarker:(GMSMarker *)marker{
        NSLog(@"begin dragging marker");
    }
    - (void)mapView:(GMSMapView *)mapView didLongPressAtCoordinate (CLLocationCoordinate2D)coordinate{
        NSLog(@"did long press at mapview");
    //when user didLongPressAtCoordinate I add a new marker on the map.
    // I want to prevent the execution of this code before the didBeginDraggingMarker method
    }

Solution

  • I solved this problem by creating a boolean property called isDragging and changing it's value depending whether a marker is being dragged.

    - (void)mapView:(GMSMapView *)mapView didBeginDraggingMarker:(GMSMarker *)marker
    {
        self.isDragging = YES;
    }
    
    - (void)mapView:(GMSMapView *)mapView didEndDraggingMarker:(GMSMarker *)marker
    {
        self.isDragging = NO;
    }
    

    Then I validate if a marker is being dragged whenever a long press is detected:

    - (void)mapView:(GMSMapView *)mapView didLongPressAtCoordinate:(CLLocationCoordinate2D)coordinate
    {
        if (self.isDragging) {
    
            return;
        }
    
        NSLog(@"Long press detected");
    }