Google maps error: Marker's position is not updated after drag

后端 未结 2 1591
清酒与你
清酒与你 2021-01-18 10:38

I set up a marker an set it\'s draggable state to true. But when I call marker.getPosition() I am always getting the same location, like marker\'s position is n

相关标签:
2条回答
  • 2021-01-18 11:11

    You should implement OnMarkerDragListener() in your main activity. And inside the method onMarkerDragEnd() you can get the position of your marker (by using its ID)

    0 讨论(0)
  • 2021-01-18 11:24

    This is an old question but since I had the same problem myself, here's the solution: you need to set the OnMarkerDragListener for the map instance first (mMap.setOnMarkerDragListener(...)) for the markers position value to get updated. You don't really need to do anything inside the listener implementation methods (you can use marker.getPosition() anywhere you like), they just have to exist.

    UPDATE

    This is also documented now in https://developers.google.com/maps/documentation/android-api/marker#marker_drag_events

    You can use an OnMarkerDragListener to listen for drag events on a marker. To set this listener on the map, call GoogleMap.setOnMarkerDragListener. To drag a marker, a user must long press on the marker. When the user takes their finger off the screen, the marker will stay in that position. When a marker is dragged, onMarkerDragStart(Marker) is called initially. While the marker is being dragged, onMarkerDrag(Marker) is called constantly. At the end of the drag onMarkerDragEnd(Marker) is called. You can get the position of the marker at any time by calling Marker.getPosition().

    0 讨论(0)
提交回复
热议问题