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();
  });
});