首页 文章

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

提问于
浏览
2

我有一个 Map ,上面有多个多边形,可以互相重叠 . 我使用https://github.com/mapbox/leaflet-pip中的 leafletPip.pointInLayer(point, layer) 来确定哪些多边形重叠 . 这发生在 processClick 函数中 . 在Vue对象中,我使用多边形创建 Map 和我的GeoJSON图层 . 我现在想要的是以下功能:如果你点击 Map 上的一个点并且这个点包含在多个多边形中,你就会有类似于选择工具的东西,例如:在弹出窗口中,单击其中一个多边形并仅触发此特定多边形的.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 回答

  • 1

    我找到了一个适合我的解决方案,但它可能不是最优雅的解决方案 . 如果点击的点包含在多个多边形中(match.length> 1),我会生成此信息字符串 . 在每次迭代中,for循环创建一个可点击链接,然后根据id调用点击函数 . 所以我基本上必须在一个字符串中生成大量HTML,并使用文字和字符串连接 . 然后我用open作为我的html参数调用openPopup函数 .

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

相关问题