[Solved] How to remove specific marker on Android GoogleMap
CODE:
private Marker mCurrentMarker;
private ArrayList<Marker> mMarkerArrayList;
@Override
public void onMapReady(final GoogleMap googleMap) {
mGoogleMap = googleMap;
mMarkerArrayList = new ArrayList<>();
googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng point) {
MarkerOptions marker_onclick = new MarkerOptions().position(
new LatLng(point.latitude, point.longitude)).title(getString(R.string.now_your_location));
if (mMarkerArrayList.size() > 0){
Marker marker_to_remove = mMarkerArrayList.get(0);
marker_to_remove.remove();
}
mCurrentMarker = mGoogleMap.addMarker(marker_onclick);
mGoogleMap.addMarker(marker_onclick);
mMarkerArrayList.add(mCurrentMarker);
}
});
}
I want that when I click on the map, there will be a marker related in showing clicked location. And marker which has been before being removed. So, there is only one marker related to showing clicked location.
I already know mGoogleMap.clean();
can clean map, also markers on the map.
But I want to remove specific marker. (Because, On my application, there are many kinds of markers. For example, a home marker is showing where the user’s home is, and the bus stop marker is showing where the bus stop is.)
So I made ArrayList and tried to use it.
But it didn’t work.
I think when i click on map, addmarker();
is working well but .remove();
seems to be not working.
Where is the error?
How can I remove specific marker only?
Solution #1:
When you add a marker on Map, you can store it into HashMap like this:
HashMap<YourUniqueKey,Marker> hashMapMarker = new HashMap<>();
Marker marker = googleMap.addMarker(markerOptions);
hashMapMarker.put(YourUniqueKey,marker);
At the time you want to delete particular marker just get your Maker by YourUniqueKey for that marker like this:
Marker marker = hashMapMarker.get(YourUniqueKey);
marker.remove();
hashMapMarker.remove(YourUniqueKey);
Solution #2:
use setOnMarkerClickListener to do this!
the code is below:
mGoogleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
marker.remove();
return true;
}
});
once you click on a “Marker” you can remove it.
Solution #3:
I went through your code and saw the bug. You are not removing markers from the arrayList.
val markerArrayList = ArrayList<Marker>()
// set map click listener
googleMap?.setOnMapClickListener {
if (markerArrayList.size > 0) {
val markerToRemove = markerArrayList.get(0)
// remove the maker from list
markerArrayList.remove(markerToRemove)
// remove the marker from the map
markerToRemove.remove()
}
// Marker options
val markerOptions = MarkerOptions().position(it).draggable(true)
// adds marker to the clicked point
val currentMarker = googleMap.addMarker(markerOptions)
// Add current marker to array list
markerArrayList.add(currentMarker)
// Set default icon
currentMarker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED))
// get the location of the clicked point
val clickedPointLatitude = it.latitude
mClickedPointLongitude = it.longitude
}
Solution #4:
This code is in Kotlin but works and it´s very easy to use.
First create a global arraylist of marks
val arrayMarker: MutableList<Marker> = ArrayList()
Then, we will populate this array everytime you place a new marker in map this way:
val mark1 = mMap.addMarker(MarkerOptions().position(latLng))
arrayMarker.add(mark1)
Ok, at this point you have the marker in map and the reference saved in array. But now to retrieve this information we will need to create this function to access data.
private fun Marker(get: Marker, position: Int): Marker {
return arrayMarker.get(position)
}
Finally, we will retrieve the data from the array and make our code work. In this sample here I’ve removed all markers like this:
val cont=0
while (cont<arrayMarker.size){
arrayMarker.get(cont).remove() //or isVisible=false if you just want to hide.
cont++
}
In case you want to delete a specific just compare the marker you want to remove (it can be done by onMarkClick) and compare with the array:
if (clickerMark == arrayMarker.get(position)) { do code here.
Hope it helps.