传单:Hide/Show 正在动态插入到地图中的标记

Leaflet: Hide/Show Markers that are being inserted dynamically into the map

我正在这样插入地图点:

function getPoints() {
  $.getJSON("get_users.php", function (data) {
    for (var i = 0; i < data.length; i++) {
      var location = new L.LatLng(data[i].lat, data[i].lng);
      var name = data[i].name;
      var website = data[i].website;
      var teste = "teste" + website;

      var marker = new L.Marker(location, {
        icon: tree1
      });
      marker.bindPopup("<a onclick='info()' style='font-size:18px; font-style: italic; font-family:courier; cursor: pointer;'>" + name + "</a><p>" + city + "</p><p id='inf' style='display:none;'>" + website + "</p><p style='font-size:10px;'>" + location + "</p>", {maxWidth: '400'});
      users.addLayer(marker);
    }
  }).complete(function() {
    if (firstLoad == true) {
      map.fitBounds(users.getBounds());
      firstLoad = false;
    };
  });
}

我希望用户只能看到标记,例如使用 class "foo",使用 javascript 隐藏没有标记的点class.

我的问题是我无法将 class 分配给标记。我已经尝试过:

使用 JQuery: $(marker._icon).addClass(foo) , $(marker).addClass('foo')

DomUtil: DomUtil.addClass(marker, 'foo') , marker = L.DomUtil.addClass(marker, 'foo')

我声明 class 是不是做错了什么?请指正!

尽管 Leaflet 确实使用 DOM 元素在地图上呈现标记,但它提供了自己的操作方式。

不要依赖 类,而是将标记添加到 图层组 ,您可以像其他图层一样使用 Leaflet 对其进行操作。您可以 addTo 映射和映射 removeLayer 所需的组以显示/隐藏其包含的标记。

您可以用您的标记创建一个 LayerGroups 数组,然后使用一个子数组,您可以只放置您想要显示的标记,并在需要时删除和添加