diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fd4f2b0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules +.DS_Store diff --git a/README.md b/README.md index 704ee92..8640c2d 100644 --- a/README.md +++ b/README.md @@ -1,24 +1,27 @@ Leaflet.functionaltilelayer =========================== +For use with Leaflet 1.0.1. + Leaflet tile layer with functionally defined URL and support for promises. A typical use case is fetching tiles asynchronously, with an ajax request or IndexedDB query. ## Usage -Use it like any other tile layer, but instead of providing a `urlTemplate` as -the first argument, provide a function. The function should return either the + +Use it like any other tile layer, but instead of providing a `urlTemplate` as +the first argument, provide a function. The function should return either the tile URL as a string, or a promise which resolves to a string. ```javascript var funcLayer = new L.TileLayer.Functional(function (view) { - var url = 'http://otile{3}.mqcdn.com/tiles/1.0.0/map/{0}/{1}/{2}.jpg' - .replace('{0}', view.zoom) - .replace('{1}', view.tile.row) - .replace('{2}', view.tile.column) - .replace('{3}', view.subdomain); - + var url = 'http://{s}.tile.osm.org/{z}/{x}/{y}.png' + .replace('{z}', view.zoom) + .replace('{y}', view.tile.row) + .replace('{x}', view.tile.column) + .replace('{s}', view.subdomain); + return url; }); ``` @@ -40,10 +43,10 @@ view = { ``` For an example of the code above, see the [basic example] -(http://ismyrnow.github.com/Leaflet.functionaltilelayer/example/basic.html). +(example/basic.html). For an example of using promises, see the [promise example] -(http://ismyrnow.github.com/Leaflet.functionaltilelayer/example/promise.html). +(example/promise.html). ## Thanks @@ -53,4 +56,4 @@ which were the inspiration for this plugin. ## License Leaflet.functionaltilelayer is free software, and may be redistributed under -the MIT-LICENSE. \ No newline at end of file +the MIT-LICENSE. diff --git a/example/basic.html b/example/basic.html index 766ffdb..e493ca3 100644 --- a/example/basic.html +++ b/example/basic.html @@ -3,8 +3,8 @@ Basic example - - + + @@ -16,18 +16,18 @@ - \ No newline at end of file + diff --git a/example/promise.html b/example/promise.html index 78f348d..f5d31ad 100644 --- a/example/promise.html +++ b/example/promise.html @@ -3,8 +3,8 @@ Promise example - - + + @@ -13,26 +13,26 @@

Tiles are rendered using promises and after a delay of 1-3 seconds.

- +
- \ No newline at end of file + diff --git a/package.json b/package.json index 2af1dca..f1b896e 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,10 @@ { "name": "leaflet.functionaltilelayer", - "version": "0.7.0", + "version": "1.0.0", "description": "Leaflet tile layer with functionally defined URL and support for promises.", - "keywords": ["leaflet"], + "keywords": [ + "leaflet" + ], "main": "src/leaflet.functionaltilelayer.js", "author": "Ishmael Smyrnow", "license": "MIT" diff --git a/src/leaflet.functionaltilelayer.js b/src/leaflet.functionaltilelayer.js index d71ed4e..3677a13 100644 --- a/src/leaflet.functionaltilelayer.js +++ b/src/leaflet.functionaltilelayer.js @@ -34,13 +34,23 @@ L.TileLayer.Functional = L.TileLayer.extend({ return this._tileFunction(view); }, - _loadTile: function (tile, tilePoint) { - tile._layer = this; - tile.onload = this._tileOnLoad; - tile.onerror = this._tileOnError; + createTile: function (coords, done) { + var tile = document.createElement('img'); - this._adjustTilePoint(tilePoint); - var tileUrl = this.getTileUrl(tilePoint); + L.DomEvent.on(tile, 'load', L.bind(this._tileOnLoad, this, done, tile)); + L.DomEvent.on(tile, 'error', L.bind(this._tileOnError, this, done, tile)); + + if (this.options.crossOrigin) { + tile.crossOrigin = ''; + } + + /* + Alt tag is set to empty string to keep screen readers from reading URL and for compliance reasons + http://www.w3.org/TR/WCAG20-TECHS/H67 + */ + tile.alt = ''; + + var tileUrl = this.getTileUrl(coords); if (typeof tileUrl === 'string') { tile.src = tileUrl; @@ -59,9 +69,11 @@ L.TileLayer.Functional = L.TileLayer.extend({ }); }); } + + return tile; } }); L.tileLayer.functional = function (tileFunction, options) { return new L.TileLayer.Functional(tileFunction, options); -}; \ No newline at end of file +};