在 Leaflet 地图上选择多个重叠要素(此处为多边形)中的一个要素

2024-03-12

我有一张地图,上面渲染了多个可以相互重叠的多边形。我用leafletPip.pointInLayer(点, 层) from https://github.com/mapbox/leaflet-pip https://github.com/mapbox/leaflet-pip用于确定哪些多边形重叠。这发生在处理点击功能。在 Vue 对象中,我使用多边形创建地图和 GeoJSON 图层。我现在想要的是以下功能:如果您单击地图上的一个点并且该点包含在多个多边形中,您将拥有类似选择工具的东西,例如在弹出窗口中,单击这些多边形之一并仅针对该特定多边形触发 .on('click') 函数。我搜索了 Leaflet 的所有功能,但找不到任何真正有用的东西。现在,如果您单击多个多边形中包含的点,则只会触发空间上包含其他多边形的多边形的 .on('click') 。

var mapVue = new Vue({
  parent: vue_broadcaster,
  el: '#map',
  data: {
    map: null,
    layer: null
  },
  created: function () {
   // Adding Leaflet map here
    var map = L.map('map').setView([51.959, 7.623], 14);
    L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
      attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
    }).addTo(map);
    this.$set('map', map);
    this.get_zones();
  },
  methods: {
    switch_zone: function (zoneid) {
      this.$dispatch('zoneSelected', zoneid)
    },
    get_zones: function () {
      this.$http.get('/api/zones/', function (data) {
      // Creation of the GeoJSON layer
        var geoZone = {
          "type": "FeatureCollection",
          "features": []
        };
        for (var i = 0; i < data['Zones'].length; i++) {
          geoZone.features.push({
            "type": "Feature",
            "geometry": {
              "type": "Polygon",
              "coordinates": [[]]
            },
            "properties": {
              "name": ""
            }
          })
          geoZone.features[i].properties.name = data['Zones'][i]['Name'];
          for (var j = 0; j < data['Zones'][i]['Geometry']['Coordinates'].length; j++) {
            geoZone.features[i].geometry.coordinates[0].push([data['Zones'][i]['Geometry']['Coordinates'][j][1], data['Zones'][i]['Geometry']['Coordinates'][j][0]])
          }
          this.layer = new L.geoJson(geoZone)
            .bindPopup(data['Zones'][i]['Name'])
            .on('click', dispatchZoneID(data['Zones'][i]['Zone-id'], this))
            .on('click', function (e) {
              processClick(e.latlng.lat, e.latlng.lng)
            })
            .addTo(this.map)
        };
      })
    }
  }
});
// function for processing the clicks on the map
function processClick(lat, lng) {
  var info = '';
  var point = [lng, lat];
  var match = leafletPip.pointInLayer(point, mapVue.layer, false);
  if (match.length) {
    for (var i = 0; i < match.length; i++) {
      info +=
      "<b>" + match[i].feature.properties.name + "<br>"
    }
  }
  if (info) {
    mapVue.map.openPopup(info, [lat, lng]);
  }
};
// not important for this one
function dispatchZoneID(id, vue) {
  return function () {
    vue.$dispatch('zoneSelected', id)
  }
};

我找到了一种适合我的解决方案,但它可能不是最优雅的解决方案。如果单击的点包含在多个多边形中(匹配长度 > 1),我生成这个info细绳。在每次迭代中,for 循环都会创建一个可点击的链接,然后根据点击调用一个函数id。因此,我基本上必须在一个字符串中生成大量 HTML,并使用文字和字符串连接。然后我打电话给打开弹出窗口功能与info作为我的 html 参数。

function processClick(lat, lng) {
  var info = '';
  var point = [lng, lat];
  var match = leafletPip.pointInLayer(point, mapVue.layer, false);
  if (match.length > 1) {
    for (var i = 0; i < match.length; i++) {
      id = match[i].feature.properties.zoneid;
      name = match[i].feature.properties.name;
      info +=
      "<b><a onclick='dispatchZoneID(\"" + id + "\")();'>"+ name + "</a><br>"
    }
  }
  else dispatchZoneID(mapVue.zoneid)();

  if (info) {
    mapVue.map.openPopup(info, [lat, lng]);
  }
};
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Leaflet 地图上选择多个重叠要素(此处为多边形)中的一个要素 的相关文章

随机推荐