Draggable marker with Google Maps SDK for iOS

好久不见. 提交于 2019-12-04 09:07:25

问题


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.



来源:https://stackoverflow.com/questions/15045687/draggable-marker-with-google-maps-sdk-for-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!