简体   繁体   English

Android google map,点击创建一个标记并立即开始拖动它

[英]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.我目前正在 android 应用程序中的谷歌 map 上长按创建标记。 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.我的问题是,当我在创建后继续按下并尝试拖动时,整个 map 被拖动并且标记没有移动。

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?有没有办法创建一个新的标记并从一开始就把它放在可拖动的 state 中,这样我就可以长按创建然后在 position 中滑动它,而不必结束我的长按并启动一个新的?

In this example after creating a marker on long press the camera move listener updates the newly added marker's position to center.在此示例中,在长按创建标记后,相机移动侦听器将新添加的标记的 position 更新为居中。

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.效果就是您所描述的,在相同的长按(仍然按住)的情况下,标记被创建并移动到相机中心,map 像往常一样移动(与标记一起)。

On release of the long-press the marker is in its final position.释放长按后,标记位于其最终 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.长按被释放,标记到达 rest - 并且 map 移动继续正常工作。 At this point the marker could be moved on its own with usual methods.此时,可以使用通常的方法自行移动标记。

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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