Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draggable marker with Google Maps SDK for iOS

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?

like image 552
user1971035 Avatar asked Feb 23 '13 21:02

user1971035


People also ask

How do I add a marker to Google Maps API?

// To add the marker to the map, call setMap(); marker.setMap(map);

How do I drag a marker on Google Maps in Swift?

By default, marker is not draggable. marker. draggable=YES; You have to press-and-hold on the marker before it will begin dragging.


2 Answers

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);
}
like image 181
Frederic Yesid Peña Sánchez Avatar answered Oct 17 '22 09:10

Frederic Yesid Peña Sánchez


Hello I just implemented a drag and drop functionality for marker in the new GoogleMaps SDK for iOS. I published the code on github under the following link: https://github.com/rweindl/google-maps-sdk-ios-drag-drop

like image 3
Robert Weindl Avatar answered Oct 17 '22 08:10

Robert Weindl