OpenLayers: How to detect the map view is completely loaded?
Basically to make sure everything is rendered on your map you need to listen for loadend events for each layer you have on the map. For wms and wfs layers this is clear and I guess you know how to do it. For the tile layers , check this example here
I eventually implemented the export function successfully. Below is the rough explanation.
- 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.
Meanwhile, OpenLayers provides a much sought after rendercomplete
event which may be handy.
postrender event seems to do the trick, like this:
map.once('postrender', function(event) {
doyourmagic();
});
Works at least from OpenLayers 3.8.2. There is fine answer there about the subject.