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?
I read: How to remove the marker in Google map v2?
Inside the Info Window of the marker, there's an HTML button (Delete) which when clicked triggers the DeleteMarker JavaScript function. This function accepts the ID of the marker as a parameter and using this ID the selected marker is removed from the Map. };
Step 1 Go to Add or Edit Map and Scroll down to the 'Infowindow Settings' section. Step 2 Enable the box of 'Hide Markers on Page Load' option. Step 3 Click on Save Map and open it in browser.
For adding a custom marker to Google Maps navigate to the app > res > drawable > Right-Click on it > New > Vector Assets and select the icon which we have to show on your Map. You can change the color according to our requirements. After creating this icon now we will move towards adding this marker to our Map.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With