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?