Drag marker in Android Google Maps

1

It is possible to do this in android studio, I have my map but I need the user to drag the marker to specify something. I know how to get my current lat and lng but how can I implement the drag?.

    
asked by DoubleM 05.12.2017 в 10:51
source

1 answer

2

Excerpted from Google Maps Bookmarks

Markers are designed to be interactive. They receive click events by default, and are often used in conjunction with event receivers to activate information windows. Setting the property draggable to the value true allows the user to modify the position of the marker. Applies a long press to activate the ability to move the marker.

Make a bookmark crawl

static final LatLng PERTH = new LatLng(-31.90, 115.86);
Marker perth = mMap.addMarker(new MarkerOptions()
                          .position(PERTH)
                          .draggable(true));

Tracking events for bookmarks.

You can use a OnMarkerDragListener to hear tracing events from a bookmark. To set this receiver on the map, call GoogleMap.setOnMarkerDragListener .

To drag a marker, a user must press and hold the marker for a long time. When the user removes his finger from the screen, the marker will remain in the determined position. When a marker is dragged, it is initially called onMarkerDragStart(Marker) .

When the marker is dragged, onMarkerDrag(Marker) is called constantly. At the end of the drag operation, onMarkerDragEnd(Marker) is called. You can get the marker position at any time by calling Marker.getPosition() .

 myMap.setOnMarkerDragListener(this);

 @Override
 public void onMarkerDrag(Marker marker) {
    Log.d(TAG, "Marker " + marker.getId() + " Drag@" + marker.getPosition());
 }

 @Override
 public void onMarkerDragEnd(Marker marker) {
    Log.d(TAG,"Marker " + marker.getId() + " DragEnd");
 }

 @Override
 public void onMarkerDragStart(Marker marker) {
    Log.d(TAG,"Marker " + marker.getId() + " DragStart");
 }

A more complete example Google Maps Android API v2 example: Draggable Marker

  

Note: By default, bookmarks can not be moved. The   possibility for a user to drag a marker must be established   specifically. This is possible with MarkerOptions.draggable (boolean)   before adding the marker to the map, or with   Marker.setDraggable (boolean) once it has been added to this.

    
answered by 05.12.2017 / 11:37
source