https://github.com/ghybs/leaflet.tilelayer.fallback
Replaces missing Tiles by scaled lower zoom Tiles
https://github.com/ghybs/leaflet.tilelayer.fallback
javascript leaflet leaflet-plugins missing-tiles tile
Last synced: 11 months ago
JSON representation
Replaces missing Tiles by scaled lower zoom Tiles
- Host: GitHub
- URL: https://github.com/ghybs/leaflet.tilelayer.fallback
- Owner: ghybs
- License: apache-2.0
- Created: 2015-11-20T07:32:06.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2019-06-06T12:12:39.000Z (over 6 years ago)
- Last Synced: 2025-02-27T18:58:35.173Z (12 months ago)
- Topics: javascript, leaflet, leaflet-plugins, missing-tiles, tile
- Language: JavaScript
- Size: 50.8 KB
- Stars: 36
- Watchers: 5
- Forks: 19
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Leaflet.TileLayer.Fallback
Plugin for Leaflet. Replaces missing Tiles (404 error) by scaled lower zoom Tiles.
[Leaflet](http://leafletjs.com/) is the leading open-source JavaScript library
for mobile-friendly interactive maps.
[](https://github.com/ghybs/Leaflet.TileLayer.Fallback/releases)
[](https://www.npmjs.com/package/leaflet.tilelayer.fallback)
Size: 2 kB minified, < 1 kB gzipped.
## Requirements
- Leaflet 1.x
- For Leaflet 0.7, use the [`v0.1.1` release](https://github.com/ghybs/Leaflet.TileLayer.Fallback/releases/tag/v0.1.1) or the [`leaflet-0.7` branch](https://github.com/ghybs/Leaflet.TileLayer.Fallback/tree/leaflet-0.7)
## Demo
[TileLayer.Fallback demonstration page](https://ghybs.github.io/Leaflet.TileLayer.Fallback/examples/tileLayerFallback-demo.html)
## Usage instructions
### Quick Guide
**HTML:**
```html
```
**JavaScript:**
```javascript
var myTileLayer = L.tileLayer.fallback(url, options);
```
Now missing tiles will be automatically replaced by scaled up tiles from lower zoom levels.
### Installing the plugin
#### Local copy
1. Download the "`leaflet.tilelayer.fallback.js`" file from the [`v1.0.4` release](https://github.com/ghybs/Leaflet.TileLayer.Fallback/releases/tag/v1.0.4).
2. Place the file alongside your page.
3. Add the `script` tag (see [Quick Guide > HTML](#quick-guide)) to your page after Leaflet script.
#### CDN
You can alternatively use the free [unpkg](https://unpkg.com) CDN service, but keep in mind that it "[_is a free, best-effort service and cannot provide any uptime or support guarantees_](https://unpkg.com/#/about)".
```html
```
### Creation
Simply use the `L.tileLayer.fallback` factory instead of your regular `L.tileLayer`:
```javascript
var myTileLayer = L.tileLayer.fallback(url, options);
myTileLayer.addTo(map);
```
## API Reference
### Creation
| Factory | Description |
| :------ | :---------- |
| **L.tileLayer.fallback**( `` [urlTemplate](http://leafletjs.com/reference-1.0.2.html#tilelayer-l-tilelayer), [``](#options) options? ) | Instantiates a tile layer object given a [URL template](http://leafletjs.com/reference-1.0.2.html#tilelayer-l-tilelayer) and optionally an options object. When tile images return a 404 error, they are replaced by a scaled up tile from lower zoom. |
### Options
| Option | Type | Default | Description |
| :----- | :--- | :------ | :---------- |
| **minNativeZoom** | `Number` | 0 | Minimum zoom number the tiles source has available. If tiles are missing down to that zoom level (included), they will be replaced by the standard Error Tile (specified by [`errorTileUrl`](http://leafletjs.com/reference-1.0.2.html#tilelayer-errortileurl)). For zoom lower than `minNativeZoom`, [standard Tile Layer behaviour](http://leafletjs.com/reference-1.0.2.html#tilelayer-minnativezoom) applies, i.e. tiles are loaded from `minNativeZoom` and scaled down. |
All other [TileLayer options](http://leafletjs.com/reference-1.0.2.html#tilelayer-option) are applicable.
### Events
| Event | Data | Description |
| :---- | :--- | :---------- |
| **tilefallback** | [`TileFallbackEvent`](#tilefallbackevent) | Fired when a tile is being replaced by a scaled up tile of lower zoom. |
All other [TileLayer events](http://leafletjs.com/reference-1.0.2.html#tilelayer-crossorigin) are applicable.
#### TileFallbackEvent
| Property | Type | Description |
| :------- | :--- | :---------- |
| `tile` | `HTMLElement` | The tile element (image). |
| `url` | `String` | The **original** source URL of the tile (before any fallback is applied). |
| `urlMissing` | `String` | The missing source URL of the tile (possibly after a few fallback attempts). |
| `urlFallback` | `String` | The fallback source URL of the tile (which may turn out to be also missing). |
### Methods
All regular [TileLayer methods](http://leafletjs.com/reference-1.0.2.html#tilelayer-method) are applicable.
## Limitations
TileLayer.Fallback plugin tries to replace each missing tile by its immediate
lower zoom equivalent, and if that one is also missing, it goes to lower zoom
again; and so on until a tile image is returned by the server, or it reaches
[`minNativeZoom`](#options).
That means it has to wait for the server to return a 404 error before attempting
to replace the tile by a lower zoom equivalent. If several zoom levels are
missing, it has to wait as many times as the number of missing zooms. Therefore,
the more missing zoom levels, the more time it takes to replace a tile.
## License
[](LICENSE)
Leaflet.TileLayer.Fallback is distributed under the [Apache 2.0 License](http://choosealicense.com/licenses/apache-2.0/).