openlayersopenlayers-5

Get total number of tiles to load


I want to get the total number of tiles needed to be loaded currently.
The reason is to have a smooth loading bar for the map. I know about the demo at openlayers, but for me, that bar jumps all over the place (goes back and forth) because many times tileloadstart is called after some tiles have loaded already. That causes the max to change and brings the bar back.
So is there a way (even if it's customizing OL) to get the total number of tiles (the final number of total tileloadstart calls) at the time of calling?


Solution

  • Sorry to dig up an old question, but I was stuck on that matter today and found an easy solution. This issue is not really covered throughout the web, so I'll post a function that counts tiles whithout having to load them beforehands. It is based on the forEachTileCoord(extent, zoom, callback) method of a TileGrid.

    function getTileCount(map, tileGrid) {
       let size = map.getSize();
       let view = map.getView();
       let extent = view.calculateExtent(size);
       let zoom = view.getZoom();
       let count = 0;
       tileGrid.forEachTileCoord(extent, zoom, function() {
          ++count;
       });
       return count
    }