How to use leaflet slider along with markercluster in Javascript?

此生再无相见时 提交于 2019-12-01 06:37:25

Edit March 2016:

You can now simply use the plugin Leaflet.MarkerCluster.LayerSupport to achieve this, without having to change Leaflet Slider plugin code.

See the demo LayerSupport with LeafletSlider plugin.

In your case, you would do:

var markers = L.layerGroup();

// Add all your markers into `markers` Layer Group.

// Check into MCG Layer Support!
// Add to map first before checking in.
L.markerClusterGroup.layerSupport().addTo(map).checkIn(markers);

var sliderControl = L.control.sliderControl({
  position: "topright",
  layer: markers,
  range: false,
  follow: 3
});

map.addControl(sliderControl);
sliderControl.startSlider();

Disclosure: I am the author of that plugin.


Original answer:

Refrain from adding (removing) individual markers directly to (from) the map, when they are handled by a MarkerClusterGroup which is on the map at the same time.

MCG expects to be the only group managing the markers, and if you want to show / hide some of the markers, you have to use markers.addLayer(myMarker); (or removeLayer), where markers is your MCG.

The Leaflet Time-Slider (sliderControl) plugin is therefore incompatible as-is with MCG: it directly adds (removes) markers to (from) the map. It does not know anything about your MCG.

Nevertheless, you should be able to make it compatible with MCG by replacing any map.addLayer by markers.addLayer (same for removeLayer) in the plugin code. Do not forget to add your MCG to the map.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!