leaflet layer.getbounds 不是函数

Tan*_* A. 5 leaflet

我从 geoJson 中提取了一个要素层,然后同步了一个表。当我放大每个功能时,我试图做到这一点,它将表格过滤到这些功能。下面是我的脚本不起作用。我在“if (map.getBounds().contains(layer.getBounds()))”处收到错误 我可以获得帮助吗?

var featureLayer = L.geoJson(null, {
  filter: function(feature, layer) {
    return feature.geometry.coordinates[0] !== 0 && feature.geometry.coordinates[1] !== 0;
  },
  pointToLayer: function (feature, latlng) {
    return L.marker(latlng, {
      title: feature.properties["status_title_github"],
      riseOnHover: true,
      icon: L.icon({
        iconUrl: "assets/pictures/markers/cb0d0c.png",
        iconSize: [30, 40],
        iconAnchor: [15, 32]
      })
    });
  },
  onEachFeature: function (feature, layer) {
    if (feature.properties) {
      layer.on({
        click: function (e) {
          identifyFeature(L.stamp(layer));
          highlightLayer.clearLayers();
          highlightLayer.addData(featureLayer.getLayer(L.stamp(layer)).toGeoJSON());
        },
        mouseover: function (e) {
          if (config.hoverProperty) {
            $(".info-control").html(feature.properties[config.hoverProperty]);
            $(".info-control").show();
          }
        },
        mouseout: function (e) {
          $(".info-control").hide();
        }
      });
      if (feature.properties["marker-color"]) {
        layer.setIcon(
          L.icon({
            iconUrl: "assets/pictures/markers/" + feature.properties["marker-color"].replace("#",'').toLowerCase() + ".png",
            iconSize: [30, 40],
            iconAnchor: [15, 32]
          })
        );
        legendItems[feature.properties.Status] = feature.properties["marker-color"];
      }
    }
  }
});




function syncTable() {
  tableFeatures = [];
  featureLayer.eachLayer(function (layer) {
    layer.feature.properties.leaflet_stamp = L.stamp(layer);
    if (map.hasLayer(featureLayer)) {
      if (map.getBounds().contains(layer.getBounds())) {
        tableFeatures.push(layer.feature.properties);
      }
    }
  });
  $("#table").bootstrapTable("load", JSON.parse(JSON.stringify(tableFeatures)));
  var featureCount = $("#table").bootstrapTable("getData").length;
  if (featureCount == 1) {
    $("#feature-count").html($("#table").bootstrapTable("getData").length + " visible feature");
  } else {
    $("#feature-count").html($("#table").bootstrapTable("getData").length + " visible features");
  }
}
Run Code Online (Sandbox Code Playgroud)

ghy*_*ybs 5

您很可能正在尝试getBounds标记。

您知道点要素不覆盖任何区域,因此没有理由尝试检索它们的“边界”。

在测试您的地图视口是否包含图层边界之前,请检查它是否是标记,即点类型要素

layer instanceof L.Marker
Run Code Online (Sandbox Code Playgroud)

或者:

getLatLng in layer
Run Code Online (Sandbox Code Playgroud)

或者因为您的图层来自 GeoJSON 数据并通过L.geoJSON工厂构建:

layer.feature.geometry.type === "Point"
Run Code Online (Sandbox Code Playgroud)

然后您可以以类似的方式检查该图层在当前地图视图端口中是否可见:

map.getBounds().contains(layer.getLatLng())
Run Code Online (Sandbox Code Playgroud)

顺便说一句,对于其他(即非点类型)几何图形,我认为您可能更喜欢检查它们是否位于intersects地图视图端口的边界,而不是完全包含在其中。