OpenLayers:如何检测地图视图是否已完全加载? [英] OpenLayers: How to detect the map view is completely loaded?
问题描述
我正在使用OpenLayers 3实现地图导出功能.
I'm implementing map exporting functionality using OpenLayers 3.
但是有一个问题:无法确定地图视图是否已完全加载或缺少一些图块.
But there is one problem: one cannot determine whether the map view is completely loaded or a few tiles is missing yet.
似乎没有这样的API或事件.最接近的是tileloadstart-tileloadend对.但是OpenLayers会异步加载磁贴,并且在磁贴实际加载之前不会触发tileloadstart-也就是说,在磁贴队列中排队的磁贴在实际加载之前不会触发事件.
It seems there is no such API or event. The close one is tileloadstart - tileloadend pair. But OpenLayers loads tiles asynchronously, and before the tile is actually loading the tileloadstart is not fired - that is, a tile that is queued in the tile queue does not fire the event before actually loading.
我能检测到地图视图已完全加载吗?
Hot can I detect the map view is completely loaded?
推荐答案
我最终成功实现了导出功能.下面是粗略的解释.
I eventually implemented the export function successfully. Below is the rough explanation.
- 使用
ol.source.on()
在ol.source
上注册tileloadstart
,tileloadend
,tileloaderror
事件处理程序,并开始管理切片负载计数. - 使用
ol.Map.once()
在ol.Map
上注册postcompose
事件处理程序. - 致电
ol.Map.renderSync()
.这会触发图块加载,因此从现在开始如果没有图块加载,则意味着所有图块均已加载. - 在
postcompose
事件处理程序上,使用event.context.canvas.toDataURL()
从event.context
捕获地图内容,并使用event.frameState.postRenderFunctions.push()
注册后期渲染功能(有点hacky). - 在已注册的后期渲染功能上,检查切片负载计数(可由
tileload*
事件处理程序管理).如果计数不为零,则放弃捕获的内容.否则,捕获就完成了. - 在
tileloadend
和tileloaderror
上,如果瓦片负载计数变为零,请从上面的步骤3重试.
- Register
tileloadstart
,tileloadend
,tileloaderror
event handlers on theol.source
s usingol.source.on()
, and start managing the tile load count. - Register
postcompose
event handlers on theol.Map
usingol.Map.once()
. - call
ol.Map.renderSync()
. This triggers the tile loading, so from now if there is no tile loading, it will mean that all tile have been loaded. - On
postcompose
event handler, capture the map content fromevent.context
usingevent.context.canvas.toDataURL()
, and register postrender function usingevent.frameState.postRenderFunctions.push()
(a bit hacky). - On the registered postrender function, check the tile load count (which can be managed by the
tileload*
event handlers). If the count is not zero, abandon the captured content. Else, the capture is done. - On
tileloadend
andtileloaderror
, if the tile load count becomes zero, retry from the step 3 above.
这篇关于OpenLayers:如何检测地图视图是否已完全加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!