Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android remove MarkerOptions(!) from Google maps

I'm adding my position to map using MarkerOptions as follows:

userMarker = new MarkerOptions().position(latLng).title("Current Location");
mMap.addMarker(userMarker);

How could it remove it from the map?

Back in the old days, Marker Class had a remove() method, but MarkerOptions has nothing similar... I also checked mMap (which is a GoogleMap), but no luck... :(

like image 896
Zoltan Avatar asked Jun 01 '26 18:06

Zoltan


2 Answers

The addMarker() method returns a Marker object that you can work with:

userMarker = new MarkerOptions().position(latLng).title("Current Location");
Marker myMarker = mMap.addMarker(userMarker);

And then remove your Marker doing

myMarker.remove();
like image 150
antonio Avatar answered Jun 03 '26 08:06

antonio


It appears that MarkerOptions class has a few methods that could help you like:

public MarkerOptions visible(boolean visible);

This basically sets visibility status of your marker.

MarkerOptions updatedMarker = userMarker.visible(false);

It also returns the MarkerOptions object with its new status updated!

You can find more information on this by clicking here.

I hope this helps you!

like image 38
Eenvincible Avatar answered Jun 03 '26 07:06

Eenvincible



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!