In the new Google Maps API for Android, we can add a marker, but there is no way to (easily) remove one.
My solution is to keep the markers in a map and redraw the map when I want to remove a marker, but it is not very efficient.
private final Map<String, MarkerOptions> mMarkers = new ConcurrentHashMap<String, MarkerOptions>();
private void add(String name, LatLng ll) {
final MarkerOptions marker = new MarkerOptions().position(ll).title(name);
mMarkers.put(name, marker);
runOnUiThread(new Runnable() {
@Override
public void run() {
mMap.addMarker(marker);
}
});
}
private void remove(String name) {
mMarkers.remove(name);
runOnUiThread(new Runnable() {
@Override
public void run() {
mMap.clear();
for (MarkerOptions item : mMarkers.values()) {
mMap.addMarker(item);
}
}
});
}
Does anyone have a better idea?
The method signature for addMarker
is:
public final Marker addMarker (MarkerOptions options)
So when you add a marker to a GoogleMap
by specifying the options for the marker, you should save the Marker
object that is returned (instead of the MarkerOptions
object that you used to create it). This object allows you to change the marker state later on. When you are finished with the marker, you can call Marker.remove()
to remove it from the map.
As an aside, if you only want to hide it temporarily, you can toggle the visibility of the marker by calling Marker.setVisible(boolean)
.
Answer:
Add the marker to the map like this
Marker markerName = map.addMarker(new MarkerOptions().position(latLng).title("Title"));
Then you’ll be able to use the remove method, it will remove only that marker
markerName.remove();
Answer:
to clear all scribbles in the map use
map.clear()
Answer:
if marker exist remove last marker. if marker does not exist create current marker
Marker currentMarker = null;
if (currentMarker!=null) {
currentMarker.remove();
currentMarker=null;
}
if (currentMarker==null) {
currentMarker = mMap.addMarker(new MarkerOptions().position(arg0).
icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
}
Answer:
1. If you want to remove a marker you can do it as marker.remove();
alternatively you can also hide the marker instead of removing it as
marker.setVisible(false);
and make it visible later whenever needed.
2. However if you want to remove all markers from the map Use map.clear();
Note: map.clear();
will also remove Polylines, Circles
etc.
3. If you not want to remove Polylines, Circles
etc. than use a loop to the length of marker (if you have multiple markers) to remove those Check out the Example here OR set them Visible false And do not use map.clear();
in such case.
Answer:
use the following code:
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
marker.remove();
return true;
}
});
once you click on “a marker”, you can remove it.
Answer:
If you use Kotlin language you just add this code:
Create global variables of GoogleMap
and Marker
types.
I use variable marker to make variable marker value can change directly
private lateinit var map: GoogleMap
private lateinit var marker: Marker
And I use this function/method to add the marker on my map:
private fun placeMarkerOnMap(location: LatLng) {
val markerOptions = MarkerOptions().position(location)
val titleStr = getAddress(location)
markerOptions.title(titleStr)
marker = map.addMarker(markerOptions)
}
After I create the function I place this code on the onMapReady()
to remove the marker and create a new one:
map.setOnMapClickListener { location ->
map.clear()
marker.remove()
placeMarkerOnMap(location)
}
It’s bonus if you want to display the address location when you click the marker add this code to hide and show the marker address but you need a method to get the address location. I got the code from this post: How to get complete address from latitude and longitude?
map.setOnMarkerClickListener {marker ->
if (marker.isInfoWindowShown){
marker.hideInfoWindow()
}else{
marker.showInfoWindow()
}
true
}
Answer:
Create array with all markers on add in map.
Later, use:
Marker temp = markers.get(markers.size() - 1);
temp.remove();
Answer:
Make a global variable to keep track of marker
private Marker currentLocationMarker;
//Remove old marker
if (null != currentLocationMarker) {
currentLocationMarker.remove();
}
// Add updated marker in and move the camera
currentLocationMarker = mMap.addMarker(new MarkerOptions().position(
new LatLng(getLatitude(), getLongitude()))
.title("You are now Here").visible(true)
.icon(Utils.getMarkerBitmapFromView(getActivity(), R.drawable.auto_front))
.snippet("Updated Location"));
currentLocationMarker.showInfoWindow();
Answer:
Just a NOTE, something that I wasted hours tracking down tonight…
If you decide to hold onto a marker for some reason, after you have REMOVED it from a map… getTag will return NULL, even though the remaining get values will return with the values you set them to when the marker was created…
TAG value is set to NULL if you ever remove a marker, and then attempt to reference it.
Seems like a bug to me…
Tags: androidandroid