Skip to content

Instantly share code, notes, and snippets.

@IvanSanchez
Created August 9, 2015 21:57
Show Gist options
  • Save IvanSanchez/34513a0f8810c7bceee8 to your computer and use it in GitHub Desktop.
Save IvanSanchez/34513a0f8810c7bceee8 to your computer and use it in GitHub Desktop.
L.TileLayer.XHR.js
L.TileLayer.prototype.options.xhr = false;
L.TileLayer.include({
createTile: function(coords, done) {
var tile = document.createElement('img');
if (!this.options.xhr) {
tile.onload = L.bind(this._tileOnLoad, this, done, tile);
tile.onerror = L.bind(this._tileOnError, this, done, tile);
if (this.options.crossOrigin) {
tile.crossOrigin = '';
}
tile.src = this.getTileUrl(coords);
} else {
var addr = this.getTileUrl(coords);
var xhr = new XMLHttpRequest();
xhr.open('GET', addr, true);
xhr.responseType = 'blob';
xhr.onload = function(e) {
tile.src = window.URL.createObjectURL(this.response);
this._tileOnLoad(done, tile);
}.bind(this);
xhr.onerror = L.bind(this._tileOnError, this, done, tile);
xhr.send();
}
/*
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 = '';
return tile;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment