Remove previous Marker and add new Marker in Google Map v2
Just clear the google map before adding marker. Like this:
@Override
public void onMapLongClick(LatLng latLng) {
googleMap.clear();
googleMap.addMarker(new MarkerOptions()
.position(latLng)
.title(latLng.toString())
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
}
Just creat a new marker object and before adding a new marker, remove the previous one
Marker marker;
MAP.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
@Override
public void onMapLongClick(LatLng arg0) {
if (marker != null) {
marker.remove();
}
marker = MAP.addMarker(new MarkerOptions()
.position(
new LatLng(arg0.latitude,
arg0.longitude))
.draggable(true).visible(true));
}
});
EDIT
Do the same for OnMapClick
MAP.setOnMapClickListener(new OnMapClickListener() {
@Override
public void onMapClick(LatLng point) {
// TODO Auto-generated method stub
if (marker != null) {
marker.remove();
}
marker = MAP.addMarker(new MarkerOptions()
.position(currentPosition)
.snippet(
"Lat:" + location.getLatitude() + "Lng:"
+ location.getLongitude())
.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
.title("ME"));
Log.e("lat", "" + point);
}
});
Here a simple way You just have to change the position
of the marker. Create Global Object as Marker marker;
After that add marker to map like
marker = map.addMarker(markerOptions).position(new Latlng(31.647316, 74.763791));
And after it use marker.setPosition(newlaLng);
where you need to add marker.