-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathmap.js
63 lines (55 loc) · 2.29 KB
/
map.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
'use strict';
(function (window, emr, L, undefined) {
var StorageTileLayer = L.TileLayer.extend({
_imageToDataUri: function (image) {
var canvas = window.document.createElement('canvas');
canvas.width = image.naturalWidth || image.width;
canvas.height = image.naturalHeight || image.height;
var context = canvas.getContext('2d');
context.drawImage(image, 0, 0);
return canvas.toDataURL('image/png');
},
_tileOnLoadWithCache: function () {
var storage = this._layer.options.storage;
if (storage) {
storage.add(this._storageKey, this._layer._imageToDataUri(this));
}
L.TileLayer.prototype._tileOnLoad.apply(this, arguments);
},
_setUpTile: function (tile, key, value, cache) {
tile._layer = this;
if (cache) {
tile._storageKey = key;
tile.onload = this._tileOnLoadWithCache;
tile.crossOrigin = 'Anonymous';
} else {
tile.onload = this._tileOnLoad;
}
tile.onerror = this._tileOnError;
tile.src = value;
},
_loadTile: function (tile, tilePoint) {
this._adjustTilePoint(tilePoint);
var key = tilePoint.z + ',' + tilePoint.y + ',' + tilePoint.x;
var self = this;
if (this.options.storage) {
this.options.storage.get(key, function (value) {
if (value) {
self._setUpTile(tile, key, value, false);
} else {
self._setUpTile(tile, key, self.getTileUrl(tilePoint), true);
}
}, function () {
self._setUpTile(tile, key, self.getTileUrl(tilePoint), true);
});
} else {
self._setUpTile(tile, key, self.getTileUrl(tilePoint), false);
}
}
});
emr.on('mapLoad', function (storage) {
var map = L.map('map').setView([53.902254, 27.561850], 13);
new StorageTileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {storage: storage}).addTo(map);
emr.fire('mapLoaded');
});
})(window, window.offlineMaps.eventManager, L);