Mapbox GL JS 中的 eventData 参数是什么?
What is the eventData Parameter in Mapbox GL JS?
在查看 Map
对象的 fitBounds()
方法的 the documentation 时,我注意到参数之一是 eventData。参数的描述是,"Data to propagate to any event listeners."
此参数被其他几种方法接受,所有这些方法都涉及某种地图移动(即 panTo
、jumpTo
、setPitch
等)。
我将如何使用这个参数?它有什么用?
当您调用 Map#fitBounds
时,Map
会触发 Map#move
等事件。如果您将 eventData
提供给 Map#fitBounds
,该数据将传递给事件的侦听器。
map.on('move', function(event) {
// event.foo is 'bar'
});
map.fitBounds(bounds, {}, {foo: 'bar'});
归功于...
卢卡斯!卢卡斯,谢谢你的回答。最终我是如何使用它的。
问题:
当为 'moveend'
使用事件侦听器时,可以看到在调用 map.fitBounds
后的动画期间会多次触发此事件。但是,只有当我们真正完成了边界的设置时,我们才想做一些事情。
解决方案:
使用 eventData
参数将自定义事件 属性 传播到我们可以用作触发器的事件侦听器。在这里,我们不调用 map.setMaxBounds
直到动画完全结束:
// Get the bounds of the bss, fit the map to the bounds
let bssBounds = getBSSBounds(bss, 1); // Custom function using turf.buffer()
// Fit the map to the above boundaries using no options, propagate event data
map.fitBounds(bssBounds, {}, {newBounds: true});
// Set max bounds
map.on('moveend', (event) => {
// If the 'moveend' event has "newBounds" != undefined
if (event.newBounds) {
// Set the maxBounds slighlty wider than the fitBounds
let maxBounds = getBSSBounds(bss, 3); // Custom function using turf.buffer()
map.setMaxBounds(maxBounds)
}
});
在查看 Map
对象的 fitBounds()
方法的 the documentation 时,我注意到参数之一是 eventData。参数的描述是,"Data to propagate to any event listeners."
此参数被其他几种方法接受,所有这些方法都涉及某种地图移动(即 panTo
、jumpTo
、setPitch
等)。
我将如何使用这个参数?它有什么用?
当您调用 Map#fitBounds
时,Map
会触发 Map#move
等事件。如果您将 eventData
提供给 Map#fitBounds
,该数据将传递给事件的侦听器。
map.on('move', function(event) {
// event.foo is 'bar'
});
map.fitBounds(bounds, {}, {foo: 'bar'});
归功于...
卢卡斯!卢卡斯,谢谢你的回答。最终我是如何使用它的。
问题:
当为 'moveend'
使用事件侦听器时,可以看到在调用 map.fitBounds
后的动画期间会多次触发此事件。但是,只有当我们真正完成了边界的设置时,我们才想做一些事情。
解决方案:
使用 eventData
参数将自定义事件 属性 传播到我们可以用作触发器的事件侦听器。在这里,我们不调用 map.setMaxBounds
直到动画完全结束:
// Get the bounds of the bss, fit the map to the bounds
let bssBounds = getBSSBounds(bss, 1); // Custom function using turf.buffer()
// Fit the map to the above boundaries using no options, propagate event data
map.fitBounds(bssBounds, {}, {newBounds: true});
// Set max bounds
map.on('moveend', (event) => {
// If the 'moveend' event has "newBounds" != undefined
if (event.newBounds) {
// Set the maxBounds slighlty wider than the fitBounds
let maxBounds = getBSSBounds(bss, 3); // Custom function using turf.buffer()
map.setMaxBounds(maxBounds)
}
});