THE 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 by clicking on the map, a marker will appear associated with the display of the clicked location. And the marker that was before the deletion. Thus, there is only one marker associated with displaying a clicked location.
I already know mGoogleMap.clean();can clear the map, as well as the markers on the map.
But I want to remove a specific marker. (Because there are many kinds of markers in my application. For example, a home marker shows where the user’s home is, and a bus stop marker indicates where the bus stop is.)
So I made an ArrayList and tried to use it.
But that did not work.
I think when I click on the map it addmarker();works fine, but .remove();it doesn't seem to work.
Where is the mistake?
?
: Google v2?