Draggable marker with Google Maps SDK for iOS

user1971035 picture user1971035 · Feb 23, 2013 · Viewed 11.4k times · Source

Has anybody figured out how to implement draggable markers with the new Google Maps SDK for iOS? The API does not provide it natively, yet. Feature request is already submitted.

If I could get a hold of the underlying view of the GMSMarker, I could intercept the Touch events. Anybody tried this?

Answer

As of today, You don't need to use external classes, just make your markers "draggable"

GMSMarker *myMarker = [[GMSMarker alloc] ...];
...
[myMarker setDraggable: YES];
// Use some kind of data to identify each marker, marker does not have 'tag' but 'userData' that is an 'id' type
[myMarker setUserData: markerId];

And implement the respective delegate

@interface YourController: UIViewController<GMSMapViewDelegate> ...

Set your delegate

GMSMapView *myMapView = [[GMSMapView alloc] ...];
...
myMapView.delegate = self;

Then you can handle each marker event, ie:

-(void)mapView:(GMSMapView *)mapView didEndDraggingMarker:(GMSMarker *)marker{
    if ([marker.userData isEqualtoString:@"xMark"])
        NSLog(@"marker dragged to location: %f,%f", marker.position.latitude, marker.position.longitude);
}