如何使用Leaflet.js中的L.control.layers激活多个L.markerClusterGroup(),而不仅仅是切换?

时间:2019-06-21 00:02:05

标签: javascript leaflet

我已经建立了一个使用MarkerClusters插件的leafletjs映射。我想选择从地图上删除一组标记。

我尝试创建多个标记聚类,然后使用L.control.layers()控制哪些聚类出现在地图上。这使我可以在两个标记簇之间切换,但不能同时显示两个标记簇,然后取消选择一个。

这是两个群集。真正的版本中确实有1000个点

var addressPoints1 = [
[47.439813,-122.289544,"point a"],
[47.650524,-122.322279,"point b"]];

var addressPoints2[
[47.631938,-122.321723,"point c"]
[47.631859,-122.321548,"point d"]];

<body>中,我有这个

        var tiles = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
                maxZoom: 18,
                attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors.'
            }),
            latlng = L.latLng(47.6, -122.32);

        var map = L.map('map', {center: latlng, zoom: 12, layers: [tiles]});

        var markers = L.markerClusterGroup();

        for (var i = 0; i < addressPoints1.length; i++) {
            var a = addressPoints1[i];
            var title = a[2];
            var marker = L.marker(new L.LatLng(a[0], a[1]), { title: title });
            marker.bindPopup(title);
            markers.addLayer(marker);
        }

    //AND
        var markers2 = L.markerClusterGroup();

        for (var i = 0; i < addressPoints2.length; i++) {
            var a = addressPoints2[i];
            var title = a[2];
            var marker2 = L.marker(new L.LatLng(a[0], a[1]), { title: title });
            marker2.bindPopup(title);
            markers2.addLayer(marker2);
        }

var overlays = { 
 "Cluster 1": markers,
"Cluster 2": markers2
 }; 

 L.control.layers(overlays).addTo(map); 

map.addLayer(markers);
    </script>

0 个答案:

没有答案