我想复选框显示隐藏全部标记。

日出
  • 22
mapresize();map=L.map('map', {editable: true, adeAnimation: false,measureControl: true, zoomControl: false,layers: [b, cities]}).setView([y,x],zoom)
var b=L.tileLayer('http://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}',{id: 'map10' },

var cities = L.layerGroup();

var overLayers = {"Cities": cities};
var layerControl=L.control.layers(baseLayers, overLayers);
map.addControl(layerControl);

上面是我的代码。没有反应。
我想复选框显示隐藏全部标记。
我的标记是鼠标点击后的。

下面是demo,我想要这个效果:
https://leafletjs.com/example...

回复
阅读 738
1 个回答
✓ 已被采纳

点击事件添加的 marker 要加载 layerGroup 上。
以下是按 demo 写的代码

<body>
    <div id="testMap"></div>
</body>
<script>
    var mp_url = 'https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}';
    var attribution = 'Map data &copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, Imagery © <a href="https://www.mapbox.com/">Mapbox</a>';
    var accessToken = 'your mapbox token';
    var grayscale = L.tileLayer(mp_url, {
        attribution,
        maxZoom: 18,
        id: 'mapbox/streets-v11',
        tileSize: 512,
        zoomOffset: -1,
        accessToken
    })
    var streets = L.tileLayer(mp_url, {
        attribution,
        maxZoom: 18,
        id: 'mapbox/streets-v9',
        tileSize: 512,
        zoomOffset: -1,
        accessToken
    })
    var cities = L.layerGroup();

    var map = L.map('testMap', {
        center: [39.73, -104.99],
        zoom: 10,
        layers: [grayscale, cities]
    });

    function onMapClick(e) {
        L.marker([e.latlng.lat, e.latlng.lng]).addTo(cities)
    }

    var baseLayers = {
        "Grayscale": grayscale,
        "Streets": streets
    };

    var overlays = {
        "Cities": cities
    };

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

    map.on('click', onMapClick);
</script>
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏