OpenLayers 3 Download Event

How can I catch an event loaded by tiles in OpenLayers 3? In OpenLayers 2, this can be done by catching the "loadend" event from the base layer of the map:

map.baseLayer.events.register('loadend' , false, function(){  });
+4
source share
2 answers

tileloadstart, tileloadendand tileloaderrorevents can be subscribed to snippet sources with OpenLayers v3.3.

You can use something similar to the following:

var tilesLoading = 0,
    tilesLoaded = 0;

tileLayer.getSource().on('tileloadend', function () {
    tilesLoaded++;
    if (tilesLoading === tilesLoaded) {
        console.log(tilesLoaded + ' tiles finished loading');
        tilesLoading = 0;
        tilesLoaded = 0;
        //trigger another event, do something etc...
    }
});

tileLayer.getSource().on('tileloadstart', function () {
    this.tilesLoading++;
});
+2
source

Now you can link this as follows until something is added to the kernel.

tileSource.setTileLoadFunction(( function(){
        var numLoadingTiles = 0;
        var tileLoadFn = tileSource.getTileLoadFunction();
        return (tile, src) => {
            console.log(src);
            if (numLoadingTiles === 0) {
                console.log('loading');
            }
            ++numLoadingTiles;
            var image = tile.getImage();

            image.onload = image.onerror =  function(){
                --numLoadingTiles;
                if (numLoadingTiles === 0) {
                    console.log('idle');
                }
            };
            tileLoadFn(tile, src);
        };
    })()); 

, : http://openlayers.org/en/v3.4.0/apidoc/ol.source.TileImage.html?unstable=true#setTileLoadFunction

+1

All Articles