How to remove markers from map cluster


#1

Hi I am using native map api and create map cluster on, but after creating cluster how to remove some markers from it, there are addmarker and addmarkers but there is no removemarkers method

how to do this without removing whole cluster? thanks


#2

any help? many thanks.


#3

The marker cluster is not designed to remove the position from the list.

But if you want to really remove it (you won’t never add the position again), try this code.

let positions: any[];

yourData.forEach((data: any, idx: number) => {
  positions.push({
    "position": {lat:..., lng: ...},
    "title": "marker_" + idx
  });
});

this.map.addMarkerCluster({
  markers: positions,
  icons: [
     ...
  ]
}).then((markerCluster: MarkerCluster) => {

  // Remove particular position (should work I guess)
  markerCluster.trigger(markerCluster.getId() + "-" + positions[0].__pgmId + "_remove");

  // or just remove a marker when you click it
  markerCluster.on(GoogleMapsEvent.MARKER_CLICK).subscribe((params: any[]) => {
    let position: ILatLng = params[0];
    let marker: Marker = params[1];
    marker.remove();
  });
});

#4

Hi,

Actually, @Jamesionic is right, there should be a way to remove Markers when using MarkerCluster.

Imagine a very simple and common scenario where you create a MarkerCluster with some initial MarkerOptions, and the app provides a feature to filter the Markers. Just for the sake of providing an example, let’s say a MarkerCluster is created with MarkerOptions for restaurants and hotels, and then the user wants to filter only for restaurants because he’s not interested in hotels. If you don’t provide a way to remove a Marker, or get the MarkerCluster’s Markers list to call Marker.remove() on the hotels’ Markers, there is no way to remove some Markers except for the solution you describe here. But as you point out there it’s a very poor solution performance-wise.

So it makes totally sense to have a Marker remove method on the MarkerCluster. You can remove a Marker when not using MarkerCluster, why shouldn’t you be able to remove a Marker when using MarkerCluster? It surprises me a lot that this simple feature is not implemented.