forked from ticinum-aerospace/leaflet-wms-header
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
46 lines (42 loc) · 1.08 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// import L from "leaflet";
'use strict';
async function fetchImage(url, callback, headers) {
let _headers = {};
headers.forEach(h => {
_headers[h.header] = h.value;
});
const f = await fetch(url, {
method: "GET",
headers: _headers,
mode: "cors"
});
const blob = await f.blob();
callback(blob);
}
L.TileLayer.CustomHeader = L.TileLayer.extend({
initialize: function (url, options, headers) {
L.TileLayer.prototype.initialize.call(this, url, options);
this.headers = headers;
},
createTile(coords, done) {
const url = this.getTileUrl(coords);
const img = document.createElement("img");
img.setAttribute("role", "presentation");
fetchImage(
url,
resp => {
const reader = new FileReader();
reader.onload = () => {
img.src = reader.result;
};
reader.readAsDataURL(resp);
done(null, img);
},
this.headers
);
return img;
}
});
L.TileLayer.customHeader = function (url, options, headers, abort) {
return new L.TileLayer.CustomHeader(url, options, headers, abort);
};