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 not updated after drag end.
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map_place))
.getMap();
LatLng MYLOCATION = new LatLng(Double.valueOf(myLat), Double.valueOf(myLng));
marker = new MarkerOptions()
.position(MYLOCATION)
.title(getString(R.string.map_title_my_location)).draggable(true);
mMap.addMarker(marker).showInfoWindow();
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(MYLOCATION, 18));
close.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
LatLng pos = marker.getPosition();
Toast.makeText(activity, "Lat: " + pos.latitude + "; Long: " + pos.longitude, Toast.LENGTH_LONG).show();
}
});
as class vars I defined:
GoogleMap mMap;
MarkerOptions marker;
Any idea?!
Open or create a map. Click an existing place on the map. In the bottom right of the box that appears, use the icons to make changes. Move place: Drag the feature on the map.
You can add a simple marker to the map at a desired location by instantiating the marker class and specifying the position to be marked using latlng, as shown below.
Long press the marker to enable dragging. When you take your finger off the screen, the marker will remain in that position. Markers are not draggable by default. You must explicitly set the marker to be draggable either with MarkerOptions.
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().
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With