是否存在判断当前 tileLayer 是否**已经**加载的函数?

Does there exist a function to tell if the current tileLayer is **already** loaded?

我想以编程方式 flyTo 到一个新位置,然后一旦到达新位置 并且 加载新位置中的图块,我想进行下一步。

如果新位置非常接近原点,则使用 load 事件将不起作用,因为它们已经加载,因此不会触发新的 load 事件。尽管如此,代码仍然需要处理是否加载可见图块,因为新位置可能离原点很远。

如何判断当前图块图层是否已经加载?类似于 map.whenReady,如果地图 已经 就绪,它会立即触发,不像 tileLayer.on("load"),如果图块层已经加载则不会触发。

无效的示例代码:

map.on("moveend zoomend", function(){
    baseLayer.on("load", function(){
        // do something
    });
});
map.flyTo(latlng);

如果切片层已经加载,“加载”事件将不会触发,并且do something不会被执行。

我想要的:

map.on("moveend zoomend", function(){
    baseLayer.whenLoaded(function(){
        // do something
    });
});
map.flyTo(latlng);

如何在 Leaflet 中实现这一点?

这不是最优雅的解决方案,但我认为您可以使用 TileLayer isLoading() 方法解决您的问题 returns true if any tile in the grid层尚未完成加载。

map.on("moveend zoomend", function(){

    baseLayer.on("load", function(){
        doSomething("load: YES");
    });

    if (!baseLayer.isLoading()) doSomething("load: NO");

});

function doSomething(info) {
    alert("do something - " + info);
}

正如在 github 上已经 answered 一样,使用 Leaflet 扩展方法可以很容易地在您自己的代码中实现 whenReady,如下所示:

L.GridLayer.include({
    whenReady: function (callback, context) {
        if (!this._loading) {
            callback.call(context || this, {target: this});
        } else {
            this.on('load', callback, context);
        }
        return this;
    },
});