简体   繁体   中英

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

I am currently creating markers on long press on a google map in an android application. I am also dragging those markers by doing the standard long press on a marker, so it lifts up and becomes dragable. Both these things work fine. My issue is that when I keep pressing, after creating, and try to drag, the whole map is dragged and the marker is not moved.

This is obviously because the touch event is not started on this new marker, that I just created. I have to lift my finger and then long press again to start dragging.

Is there a way to create a new marker and put it in the draggable state from the start, so I can long press to create and then slide it in position without having to end my long press and initiate a new one?

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.

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM