如何创建具有单独点击功能的多条折线?

时间:2019-04-27 12:55:34

标签: leaflet

我正在迭代脚本中包含折线数据的对象数组。然后,将迭代对象添加到地图中。问题是,由于定义变量被上次迭代覆盖,因此我无法为迭代对象分配不同的onclick事件


for (let i = 0; i < this.$store.state.streets.length; i++) {
var currentIteratedStreet = this.$store.state.streets[i]
var latlngs = currentIteratedStreet.latlngs
var polyline = L.corridor(latlngs, {
    color: '#0068ff',
    opacity: 0.5,
    corridor: 5,
    lineCap: 'square',
}).addTo(mymap)

polyline.on('click', () => {
    console.log(polyline.options)
})
}

var polyline总是被最后一次迭代(预期的)覆盖,并为每条折线打印相同的输出。我找不到任何使我能够为每个折线添加单独的onclick函数的源。我将不胜感激。非常感谢!

1 个答案:

答案 0 :(得分:0)

您可以将参数添加到click触发的函数中以处理该事件,并检索使用event.target触发该事件的对象:

polyline.on('click', (event) => {
    console.log(event.target.options);
})