Draggable marker with Google Maps SDK for iOS

2019-03-15 01:03发布

问题:

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?

回答1:

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);
}


回答2:

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



回答3:

Swift 3.0

Sweet! All I had to do was add this variable to my marker. Then when carefully select and hold the marker for a moment you can drag it around.

marker.isDraggable = true  

I have not yet implemented the delegates to save my marker data but here's what it should look like.

extension MapVC : GMSMapViewDelegate {

    func mapView (_ mapView: GMSMapView, didEndDragging didEndDraggingMarker: GMSMarker) {

        print("Drag ended!")
        print("Update Marker data if stored somewhere.")

    }

}

Note: For a while I was trying to un-animate the screen and marker from floating and animating UP after selected. Now I see that it's a "feature" since your finger is in the way and you can't really see well if it doesn't move the screen during a drag. (Duh!)



回答4:

Draggable markers have not yet been added to the SDK. Please file a feature request.