mapbox-gl-js icon indicating copy to clipboard operation
mapbox-gl-js copied to clipboard

Tile Layer progress bar / get amount of tiles loaded

Open SteepAtticStairs opened this issue 2 years ago • 0 comments

There is a plugin for Leaflet called leaflet-tile-loading-progress-control, and it does exactly what the title suggests. When a Tile Layer with {z} {x} {y} are loaded, or a WMS tile layer is loaded, a progress bar will show up on the map indicating the percentage of tiles loaded.

My question is if there is a similar functionality in GL JS, or if not directly, how I could implement this.

Ideally there would be a way to get the total amount of tiles that the browser will request, and this would probably fire after a moveend or a zoomend event, as that is when the tiles are "reloaded," or rather, new ones are fetched. You would then also have to know how many tiles the browser had already loaded, and then just do tilesAlreadyLoaded / numberOfTotalTiles to get your decimal percentage.

If this is not possible, is there a way to ONLY get the amount of tiles the browser has loaded? This could also show the user that the map is actually loading tiles, as all I have right now is showing a spinner on the dataloading event, and hiding it on idle:

map.on('dataloading', () => {
    toggleSpinner(true);
});
map.on('idle', () => {
    toggleSpinner(false);
});

Here are the tile layers that I would be using this functionality with:

// WMS layer
map.addLayer({
    'id': `wms-test-layer`,
    'type': 'raster',
    'source': {
        'type': 'raster',
        'tiles': [
            `https://mesonet.agron.iastate.edu/cgi-bin/wms/nexrad/n0q.cgi?service=WMS&request=GetMap&layers=nexrad-n0q-900913&styles=&format=image%2Fpng&transparent=true&version=1.1.1&width=256&height=256&srs=EPSG:3857&bbox={bbox-epsg-3857}`
        ],
        'tileSize': 256
    },
    'paint': {
        'raster-opacity': opacity,
        "raster-fade-duration": 0
    }
});

// Tile layer
map.addLayer({
    id: 'rpv',
    type: "raster",
    source: {
        type: "raster",
        tiles: [
            `https://mesonet.agron.iastate.edu/cache/tile.py/1.0.0/ridge::LWX-N0B-0/{z}/{x}/{y}.png`
        ],
        tileSize: 256
    },
});

Any ideas on how to do this would be awesome, because I don't think there is a way to do it with the information the documentation provides.

SteepAtticStairs avatar Jul 06 '22 20:07 SteepAtticStairs