Google maps error: marker position does not update after drag and drop

I set the marker and dragged it to true. But when I call marker.getPosition() , I always get the same location, since the position of the marker is not updated after the end is complete.

 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(); } }); 

how the vars I classes are defined:

 GoogleMap mMap; MarkerOptions marker; 

Any idea ?!

+4
source share
2 answers

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

UPDATE

This is also described at https://developers.google.com/maps/documentation/android-api/marker#marker_drag_events

You can use OnMarkerDragListener to listen for drag events on the marker. To install this listener on a map, call GoogleMap.setOnMarkerDragListener. To drag a marker, the user must press the marker for a long time. When the user moves his finger away from the screen, the marker will remain in this position. When the marker is dragged, onMarkerDragStart (Marker) is first called. While the marker is being dragged, onMarkerDrag (Marker) is invoked continuously. At the end, drag onMarkerDragEnd (Marker) is called. You can get the marker position at any time by calling Marker.getPosition ().

+10
source

You must implement OnMarkerDragListener() in your main action. And inside the onMarkerDragEnd() method, you can get the position of your marker (using your identifier)

+6
source

All Articles