本文介绍了传单layer.getbounds不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个来自geoJson的要素图层,然后同步了一个表.当我放大每个功能时,我试图将其过滤,以将表格过滤到那些功能.下面是我的脚本不起作用.我在'if(map.getBounds().contains(layer.getBounds()))处收到错误消息?

I have a feature layer pull from geoJson and then syncing a table. I'm trying to make it when I zoom in on eachFeature, it filters the table to those features. Below is my script that is not working. I'm getting the error at 'if (map.getBounds().contains(layer.getBounds()))' Can I get some help?

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");
  }
}

推荐答案

很可能您正尝试在标记上getBounds.

Most probably you are trying to getBounds on a marker.

您了解点要素不会覆盖任何区域,因此应该没有理由尝试获取其边界".

You understand that point features do not cover any area, therefore there should be no reason to try retrieving their "bounds".

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

Before testing if your map viewport contains the layer bounds, check whether it is a Marker or not, i.e. a point type feature

layer instanceof L.Marker

或者:

getLatLng in layer

或者由于您的图层来自GeoJSON数据并且是通过L.geoJSON工厂构建的:

Or since your layers come from GeoJSON data and are built through L.geoJSON factory:

layer.feature.geometry.type === "Point"

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

Then you can check if that layer is visible in your current map view port in a similar way:

map.getBounds().contains(layer.getLatLng())

对于其他(即非点类型)几何,

顺便说一句,我认为您可能更希望检查其边界是否在地图视图端口的范围内,而不是完全包含在其中.

BTW for other (i.e. non point type) geometries, I think you would probably prefer checking if their bounds intersects the map view port, rather than is completely contained within.

这篇关于传单layer.getbounds不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-04 11:39