Eliminar un marcador de un GoogleMap
En la nueva API de Google Maps para Android, podemos agregar un marcador, pero no hay forma de eliminar uno (fácilmente).
Mi solución es mantener los marcadores en un mapa y volver a dibujar el mapa cuando quiero eliminar un marcador, pero no es muy eficiente.
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);
}
}
});
}
¿Alguien tiene una idea mejor?
9 answers
La firma del método para addMarker
is:
public final Marker addMarker (MarkerOptions options)
Así que cuando agrega un marcador a un GoogleMap
especificando las opciones para el marcador, debe guardar Marker
objeto devuelto (en lugar del objeto MarkerOptions
que utilizó para crearlo). Este objeto le permite cambiar el estado del marcador más adelante. Cuando haya terminado con el marcador, puede llamar Marker.remove()
para eliminarlo del mapa.
Como un aparte, si solo quieres ocultarlo temporalmente, puede cambiar la visibilidad del marcador llamando Marker.setVisible(boolean)
.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-12-03 22:23:08
Para borrar todos los garabatos en el mapa use
map.clear()
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-10-13 01:39:08
Agregue el marcador al mapa de esta manera
Marker markerName = map.addMarker(new MarkerOptions().position(latLng).title("Title"));
Entonces usted será capaz de utilizar el método remove, se eliminará solo ese marcador
markerName.remove();
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-12-25 21:20:57
Si existe marcador, elimine el último marcador. si el marcador no existe, cree el marcador actual
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)));
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-07-07 07:08:07
1. Si quieres eliminar un marcador puedes hacerlo como marker.remove();
alternativamente, también puede ocultar el marcador en lugar de eliminarlo como
marker.setVisible(false);
Y hacerlo visible más tarde cuando sea necesario.
2. sin Embargo, si desea eliminar todos los marcadores del mapa Uso map.clear();
Nota: map.clear();
también quite Polylines, Circles
etc.
3. Si no desea eliminar Polylines, Circles
etc. luego use un bucle a la longitud del marcador (si tiene varios marcadores) para eliminarlos Echa un vistazo al Ejemplo aquí O ponlos Visibles false Y no utilices map.clear();
en tal caso.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 12:10:41
Utilice el siguiente código:
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
marker.remove();
return true;
}
});
Una vez que haga clic en "un marcador", puede eliminarlo.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-09-01 13:53:00
Hacer una variable global para realizar un seguimiento del marcador
private Marker currentLocationMarker;
/ / Eliminar el marcador antiguo
if (null != currentLocationMarker) {
currentLocationMarker.remove();
}
/ / Añadir marcador actualizado y mover la cámara
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();
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-01-11 08:50:45
Sólo una NOTA, algo que perdí horas rastreando esta noche...
Si decide mantener un marcador por alguna razón, después de haberlo ELIMINADO de un mapa... getTag devolverá NULL, a pesar de que los valores get restantes retornarán con los valores que los configuró cuando se creó el marcador...
El valor de la etiqueta se establece en NULL si alguna vez elimina un marcador y luego intenta hacer referencia a él.
Me parece un error...
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-01-25 05:32:49
Crear matriz con todos los marcadores en añadir en el mapa.
Más tarde, use:
Marker temp = markers.get(markers.size() - 1);
temp.remove();
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-05-28 23:19:39