简体   繁体   English

OpenLayers 3捕获tile加载的事件

[英]OpenLayers 3 catch tiles loaded event

How can I catch the tiles loaded event in OpenLayers 3? 如何在OpenLayers 3中捕获tile加载的事件? In OpenLayers 2 this could be done by catching the "loadend" event from the baselayer of the map: 在OpenLayers 2中,这可以通过从地图的底层捕获“loadend”事件来完成:

map.baseLayer.events.register('loadend' , false, function(){  });

tileloadstart , tileloadend , and tileloaderror events can be subscribed to on tile sources since OpenLayers v3.3. 自OpenLayers v3.3起, tileloadstarttileloadendtileloaderror事件可以在tile源上订阅。

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++;
});

You can hook this up in the following way as of now, until something is added into the core. 您可以按照以下方式将其挂钩,直到将某些内容添加到核心中。

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);
        };
    })()); 

You can see all the tile source classes that this can be used for here: http://openlayers.org/en/v3.4.0/apidoc/ol.source.TileImage.html?unstable=true#setTileLoadFunction 您可以在此处查看可用于此的所有tile源类: http//openlayers.org/en/v3.4.0/apidoc/ol.source.TileImage.html?unstable = true#setTileLoadFunction

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM