Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?!

like image 270
Ivan Bajalovic Avatar asked Feb 12 '13 09:02

Ivan Bajalovic


People also ask

How do I drag a location on Google Maps?

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.

How do I get a marker position on Google Maps?

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.

How do I drag a marker on Google Maps Android?

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.


2 Answers

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().

like image 145
Rene Juuse Avatar answered Sep 17 '22 08:09

Rene Juuse


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)

like image 42
Blubar Avatar answered Sep 19 '22 08:09

Blubar