gpt4 book ai didi

javascript - 将参数传递给 oneeachfeature 传单

转载 作者:搜寻专家 更新时间:2023-10-30 21:29:01 24 4
gpt4 key购买 nike

我试图通过点击一个多边形来传递一个参数来绑定(bind)它,我有以下内容:

var mapLayer = new L.TopoJSON(jsonMap, {style: style, onEachFeature: onEachFeature.bind(null,null,selectionManager), pane:'borders'}).addTo(this.map); 


function onEachFeature(feature, layer, selectionManager) {
console.log(selectionManager)
layer.on({
mouseover: highlightFeature,
mouseout: resetHighlight,
click: dataFilter.bind(null,ID)
});
}

function dataFilter(selectionManager,e){
var layer = e.target;
var zoneName = layer.feature.properties.Index;
console.log(zoneName)
console.log(selectionManager);
}

所以我的目标是读取 dataFilter 中的一个参数(在本例中是 selectionManager)

最佳答案

根据 Leaflet 文档,onEachFeature 需要是一个接收两个参数(featurelayer)的函数。使用 Function.prototype.bind在您使用它的方式中,它并没有按照您的意愿行事。

相反,创建一个 closure :

function onEachFeatureClosure(dataFilter) {
return function onEachFeature(feature, layer) {
// Your own logic, that uses dataFilter as well as feature and layer
}
}

L.topoJSON(jsonMap, {
style: style,
onEachFeature: onEachFeatureClosure(selectionManager),
pane:'borders'
}).addTo(this.map);

请注意,onEachFeatureClosure(selectionManager) 的返回值是一个类似于 function onEachFeature(feat, layer) {...} 的函数。

关于javascript - 将参数传递给 oneeachfeature 传单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46580213/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com