[GIS] Leaflet – what to do when your maptiles don’t load

leaflettiles

I'm using Leaflet, and using OSM's tile.openstreetmap.org tile server. However, sometimes my application does not load the maps tiles. It seems OSM's servers are having some problems sporadically, which is out of my scope of fixing. So, my question:

Is it possible to wait an X amount of time for the tile server to load the requested tiles, before trying to contact another URL for its tiles?

Best Answer

I don't think Leaflet supports this out of the box. However, I think you can implement such a behavior using events such as tileloadstart and tileload: http://leafletjs.com/reference.html#tilelayer-tileloadstart

For example, you could start counting time the first time a tileloadstart is fired using setTimeout, and cancel it (using clearTimeout) once a tileload is fired. If nobody cancels the setTimeout, swap the base OSM layer for another one.