Remove Previous Marker And Add New Marker In Google Map V2
I want to remove a current marker after long click on map anywhere and recreate a new marker at that point. I have cleared google map on long click on map and new marker is created
Solution 1:
Just creat a new marker object and before adding a new marker, remove the previous one
Marker marker;
MAP.setOnMapLongClickListener(newGoogleMap.OnMapLongClickListener() {
@OverridepublicvoidonMapLongClick(LatLng arg0) {
if (marker != null) {
marker.remove();
}
marker = MAP.addMarker(newMarkerOptions()
.position(
newLatLng(arg0.latitude,
arg0.longitude))
.draggable(true).visible(true));
}
});
EDIT
Do the same for OnMapClick
MAP.setOnMapClickListener(newOnMapClickListener() {
@OverridepublicvoidonMapClick(LatLng point) {
// TODO Auto-generated method stubif (marker != null) {
marker.remove();
}
marker = MAP.addMarker(newMarkerOptions()
.position(currentPosition)
.snippet(
"Lat:" + location.getLatitude() + "Lng:"
+ location.getLongitude())
.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
.title("ME"));
Log.e("lat", "" + point);
}
});
Solution 2:
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)));
}
Solution 3:
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.
Solution 4:
At first clear the Google map, then add a new marker. Check the code below
mMap.clear();
//google map marker adding code here
Solution 5:
Please try the blow code :-
// Global Variable...privateMarker mPreviousMarker ;
@OverridepublicbooleanonMarkerClick(Marker marker) {
if (mPreviousMarker != null) {
mPreviousMarker.remove();
}
mPreviousMarker = googleMap.addMarker(newMarkerOptions().position(latLng).icon(bitmapDescriptor));
}
LatLng :- Your latlong where you want to add and bitmapDescroptor is icon. {Just for understanding}
Post a Comment for "Remove Previous Marker And Add New Marker In Google Map V2"