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.
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.
tileloadstart
, tileloadend
, tileloaderror
event handlers on the ol.source
s using ol.source.on()
, and start managing the tile load count.postcompose
event handlers on the ol.Map
using ol.Map.once()
.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.postcompose
event handler, capture the map content from event.context
using event.context.canvas.toDataURL()
, and register postrender function using event.frameState.postRenderFunctions.push()
(a bit hacky).tileload*
event handlers). If the count is not zero, abandon the captured content. Else, the capture is done.tileloadend
and tileloaderror
, if the tile load count becomes zero, retry from the step 3 above.