首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >用Leaflet.markercluster替换标记

用Leaflet.markercluster替换标记
EN

Stack Overflow用户
提问于 2019-06-21 03:22:03
回答 1查看 220关注 0票数 1

我正在用一张传单地图来展示世界各地的火山。单击切换按钮时,将显示所有标记。当点击切换按钮时,标记应该会出现。

在这里,我想应用集群标记。它不起作用。

代码语言:javascript
复制
//Creating map options
    var mapOptions = {
        center: [40.4168, -3.7038],
        zoom: 2,
        minZoom: 2,
        maxZoom: 18,
        maxBounds: [
            [-75, -190],
            [90, 190]
        ],
        maxBoundsViscosity: 0.5,
    }

    // Creating a map object
    var map = new L.map('map', mapOptions);

    // Add Tile Layer and add to map
    L.tileLayer('https://api.maptiler.com/maps/streets/{z}/{x}/{y}.png?key=8vFNrApGjV6jRicu4ins').addTo(map);

    //Adding geoJson data and adding the marker and popup

    var geojsonMarkerOptions = {
        radius: 3,
        fillColor: "#FF0000",
        color: "#000",
        weight: 1,
        opacity: 0.2,
        fillOpacity: 0.5
    };
    //Marker Cluster

    var markerClusters = L.markerClusterGroup();

    for ( var i = 0; i < volcano.length; ++i )
    {
      var popup = volcano[i].name +
                  '<br/>' + volcano[i].properties. NAME_+
                  '<br/><b>Type:</b> ' + volcano[i].properties.TYPE_ +
                  '<br/><b>Location:</b> ' + volcano[i].properties.LOCATION;


      var m = L.marker( [volcano[i].lat, volcano[i].lng], {icon: geojsonMarkerOptions} )
                      .bindPopup( popup );

      markerClusters.addLayer( m );
    }

    map.addLayer( markerClusters );


    //-Creating interactive buttons:Toggler button to show on/off worldwide volcanoes

    var volcanoPoints = null;


    // Create event listener for add Volcanoes Worldwide Button
    document.getElementById("addButton").addEventListener("click", addVolcanoWorldwide);

    // Add volcano worldwide function
    function addVolcanoWorldwide() {
        volcanoPoints.addTo(map);
    };

    function addVolcanoWorldwide() {
        if (map.hasLayer(volcanoPoints)) {
            removeVolcanoWorldwide();
        };
        volcanoPoints = L.geoJson(volcano, {
            pointToLayer: function(feature, latlng) {
                return L.circleMarker(latlng, geojsonMarkerOptions);
            },
            onEachFeature: volcanoSearch
        }).addTo(map);
    };


    // Create event listener for the remove Volcanoes Worldwide Button
    document.getElementById("removeButton").addEventListener("click", removeVolcanoWorldwide);

    // Remove volcano worldwide function
    function removeVolcanoWorldwide() {
        volcanoPoints.remove(map);
    };

    document.getElementById("toggleButton").addEventListener("click", toggleVolcanoes);

    // Toggle Volcanoes
    function toggleVolcanoes() {
        if (map.hasLayer(volcanoPoints)) {
            removeVolcanoWorldwide();
        }
        else {
            addVolcanoWorldwide();
        }
    };
EN

回答 1

Stack Overflow用户

发布于 2019-06-21 09:49:11

欢迎来到SO!

您的代码显示您从volcano构建了两组独立的标记:-对于MarkerClusterGroup,作为具有经度和lng属性的对象数组-对于切换要素,作为传递给Leaflet GeoJSON图层组工厂的GeoJSON要素数组。

如果您的volcano包含一组兼容的GeoJSON特性,那么第一组(针对MarkerClusterGroup)应该不会按照当前的编码方式工作。

如果您希望您的切换功能直接处理您的MarkerClusterGroup,那么只需将您的volcanoPoints添加到MCG中,而不是添加到地图中。您也可以只构建一次,然后切换MCG。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56692610

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档