How Does One Implement Drag and Drop for Android Marker

How does one implement drag and drop for Android marker?

Here is a sample project from one of my books showing drag-and-drop movement of markers on a Google Map in Android.

In a nutshell, it uses onTouchEvent() to detect when the user touches and holds their finger near a marker. It then removes the marker from the overlay, but puts the same image over top of the map using RelativeLayout. Then, on "move" touch events, the image is moved (faster than forcing the whole overlay to redraw). When the finger is lifted, the image is removed, but the marker is put back in the overlay at the new spot.

Make marker draggable after single click in Android

@user2595870:

Hello,

I know it should be in comment but due to less reputations i am suggesting you in answer.

You want to drag and drop facility for marker kind of thing ?

if so there is good example provided here. Please check it.

Drag and drop button on google maps to create marker

I think you found the dropped position because, in your question, you want to simulate click on the specific position, therefore my answer is:

You can find the LatLng by dropped position

val projection = googleMap.getProjection()

// Returns the geographic location that corresponds to a screen location
val latLng = projection.fromScreenLocation(Point(yourX, yourY))

and after that is easy to create the marker

 // Creating a marker
val markerOptions = MarkerOptions()

// Setting the position for the marker
markerOptions.position(latLng)

Android google map, Create a marker on click and immediately start dragging it

In this example after creating a marker on long press the camera move listener updates the newly added marker's position to center.

The effect is what you described in that with the same long press (still holding down) the marker is created and is moved to camera center and map moves (along with marker) as usual.

On release of the long-press the marker is in its final position.

The idle listener is used to stop updating the newly added marker on camera moves. The marker can still be dragged on its own with the usual marker drag events (after the initial long press).

Applicable parts:

private Marker addedMarker;

@Override
public void onMapReady(GoogleMap map) {
gMap = map;

// ... other stuff...

// on long press, create a marker at the press point, set the
// the marker as draggable and record the new marker object.
//
// The camera moves will use the marker object to move it (while still
// in long press mode).
gMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
@Override
public void onMapLongClick(LatLng lng) {
Log.d(TAG,"On long click");
MarkerOptions mo = new MarkerOptions();
mo.position(lng).draggable(true);
addedMarker = gMap.addMarker(mo);

}
});

// If the added marker is still valid then update its position to
// camera center.
gMap.setOnCameraMoveListener(new GoogleMap.OnCameraMoveListener() {
@Override
public void onCameraMove() {
LatLng pos = gMap.getCameraPosition().target;
if (addedMarker != null) {
addedMarker.setPosition(pos);
}
}
});

// When the long-press drag stops then stop updating the newly added
// marker. The marker can still be dragged on its own as a result of
// the draggable setting.
gMap.setOnCameraIdleListener(new GoogleMap.OnCameraIdleListener() {
@Override
public void onCameraIdle() {
addedMarker = null;
}
});

}

Video of above - hard to tell but the marker is created while holding the long press and camera is moved while still holding the long press.

The long press is released and the marker comes to rest - and map moves continue to work as normal. At this point the marker could be moved on its own with usual methods.

Sample Image

How to implement a draggable ExtendedOverlayItem on OSMDroid?

I solved my problem by updating the OSMBonusPack Library. And the following code was used to implement the draggable marker:

import org.osmdroid.bonuspack.overlays.Marker;
import org.osmdroid.bonuspack.overlays.Marker.OnMarkerDragListener;
import org.osmdroid.util.GeoPoint;

public static void applyDraggableListener(Marker poiMarker) {
poiMarker.setDraggable(true);
poiMarker.setOnMarkerDragListener(new OnMarkerDragListener() {
@Override
public void onMarkerDragStart(Marker marker) {}

@Override
public void onMarkerDragEnd(Marker marker) {
GeoPoint geopoint = marker.getPosition();
}

@Override
public void onMarkerDrag(Marker marker) {}
});
}


Related Topics



Leave a reply



Submit