Draggable marker with Google Maps SDK for iOS

后端 未结 4 1360
鱼传尺愫
鱼传尺愫 2021-02-05 13:33

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.

相关标签:
4条回答
  • 2021-02-05 13:56

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

    0 讨论(0)
  • 2021-02-05 14:04

    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

    0 讨论(0)
  • 2021-02-05 14:06

    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!)

    0 讨论(0)
  • 2021-02-05 14:09

    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);
    }
    
    0 讨论(0)
提交回复
热议问题