我有一张地图,上面渲染了多个可以相互重叠的多边形。我用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: '© <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)
}
};