Layer loading events

Is there any sort of events capability for layers to subscribe to when a layer is loading? This is how OpenLayers handles this:


Some of those events are available on ImageryLayerCollection. There’s currently no event for the start and end of loading, though. Can you tell me more about what you’re trying to accomplish?


Thanks for the quick reply, Kevin. What I'd like to be able to do is show some sort of loading indicator when a layer is loading. For example with the default Bing Maps layer if the user zooms and then the layer loads to show more detail I'd like to have a loading indicator.

Makes sense. You can do that in a slightly hacky way by digging into the load queue: centralBody._surface._tileLoadQueue. If you’re using the latest release version, the queue is an instance of TileLoadQueue. If you’re using master, it’s a simple array. The elements in the queue are Tile instances, which are basically terrain tiles. To figure out which imagery layers are loading, you’ll need to dig further into the imagery property of each tile, which is an array of TileImagery instances and look for ones that have an imagery.state property that is not ImageryState.READY. Clearly this is something we ought to make easier. :slight_smile:


Thanks! I'll take a look into this and see what I can figure out.


I've noticed that in the latest release _tileLoadQueue is an array as you mentioned. Can I assume that loading is finished when the array is empty? I'm looking into modifying the push method to track when items are added and either the pop or shift method when items are removed. Does this sound reasonable to you?

Yes, you can pretty much assume that an empty array means loading is done. Here’s how it actually works, though.

At the start of each render frame, the load queue is cleared. Then, during tile selection, each tile that is needed but not loaded is added to the queue. Next, the queue is processed, which means that some (but not necessarily all) of the tiles that are needed are loaded. However, loaded tiles are not removed from the queue. There’s no need, since the queue will be cleared (by setting its length to zero) at the start of the next frame, anyway.

So, when you see an empty queue, it means that an entire render frame completed without the need for any additional tiles.

I’m not completely sure what you mean when you say you’re modifying the push and pop/shift methods. If you’re hooking up events, the best way to do so is to modify CentralBodySurface to raise events at the appropriate time, rather than hooking into array methods.


My plan was to override push() and pop() on _tileLoadQueue and do my processing there (i.e. when something is pushed to the array raise the 'loading' event and when the last item is popped raise the 'done loading' event). However, it seems like that's not quite how it works and it would probably be best to modify CentralBodySurface as you suggested.