|
@@ -0,0 +1,167 @@
|
|
1
|
+/*
|
|
2
|
+ * Leaflet.GeoJSONGridLayer
|
|
3
|
+ */
|
|
4
|
+
|
|
5
|
+(function () {
|
|
6
|
+
|
|
7
|
+ var console = window.console || {
|
|
8
|
+ error: function () {},
|
|
9
|
+ warn: function () {}
|
|
10
|
+ };
|
|
11
|
+
|
|
12
|
+ function defineLeafletGeoJSONGridLayer(L) {
|
|
13
|
+ L.GeoJSONGridLayer = L.GridLayer.extend({
|
|
14
|
+ initialize: function (url, options) {
|
|
15
|
+ L.GridLayer.prototype.initialize.call(this, options);
|
|
16
|
+
|
|
17
|
+ this._url = url;
|
|
18
|
+ this._geojsons = {};
|
|
19
|
+ this._features = {};
|
|
20
|
+ this.geoJsonClass = (this.options.geoJsonClass ? this.options.geoJsonClass : L.GeoJSON);
|
|
21
|
+ },
|
|
22
|
+
|
|
23
|
+ onAdd: function (map) {
|
|
24
|
+ var layers = this._geojsons;
|
|
25
|
+ Object.keys(layers).forEach(function (key) {
|
|
26
|
+ map.addLayer(layers[key]);
|
|
27
|
+ });
|
|
28
|
+
|
|
29
|
+ L.GridLayer.prototype.onAdd.call(this, map);
|
|
30
|
+ this.zoomanimHandler = this._handleZoom.bind(this);
|
|
31
|
+ map.on('zoomanim', this.zoomanimHandler);
|
|
32
|
+ },
|
|
33
|
+
|
|
34
|
+ onRemove: function (map) {
|
|
35
|
+ var layers = this._geojsons;
|
|
36
|
+ Object.keys(layers).forEach(function (key) {
|
|
37
|
+ map.removeLayer(layers[key]);
|
|
38
|
+ });
|
|
39
|
+
|
|
40
|
+ L.GridLayer.prototype.onRemove.call(this, map);
|
|
41
|
+ map.off('zoomanim', this.zoomanimHandler);
|
|
42
|
+ },
|
|
43
|
+
|
|
44
|
+ _handleZoom: function (e) {
|
|
45
|
+ this.checkZoomConditions(e.zoom);
|
|
46
|
+ },
|
|
47
|
+
|
|
48
|
+ createTile: function (coords, done) {
|
|
49
|
+ var tile = L.DomUtil.create('div', 'leaflet-tile');
|
|
50
|
+ var size = this.getTileSize();
|
|
51
|
+ tile.width = size.x;
|
|
52
|
+ tile.height = size.y;
|
|
53
|
+
|
|
54
|
+ this.fetchTile(coords, function (error) {
|
|
55
|
+ done(error, tile);
|
|
56
|
+ });
|
|
57
|
+ return tile;
|
|
58
|
+ },
|
|
59
|
+
|
|
60
|
+ fetchTile: function (coords, done) {
|
|
61
|
+ var tileUrl = L.Util.template(this._url, coords);
|
|
62
|
+ var tileLayer = this;
|
|
63
|
+
|
|
64
|
+ var request = new XMLHttpRequest();
|
|
65
|
+ request.open('GET', tileUrl, true);
|
|
66
|
+
|
|
67
|
+ request.onload = function () {
|
|
68
|
+ if (request.status >= 200 && request.status < 400) {
|
|
69
|
+ var data = JSON.parse(request.responseText);
|
|
70
|
+ tileLayer.addData(data);
|
|
71
|
+ done(null);
|
|
72
|
+ } else {
|
|
73
|
+ // We reached our target server, but it returned an error
|
|
74
|
+ done(request.statusText);
|
|
75
|
+ }
|
|
76
|
+ };
|
|
77
|
+
|
|
78
|
+ request.onerror = function () {
|
|
79
|
+ done(request.statusText);
|
|
80
|
+ };
|
|
81
|
+
|
|
82
|
+ request.send();
|
|
83
|
+ },
|
|
84
|
+
|
|
85
|
+ getLayers: function () {
|
|
86
|
+ var geojsons = this._geojsons,
|
|
87
|
+ layers = [];
|
|
88
|
+ Object.keys(geojsons).forEach(function (key) {
|
|
89
|
+ layers.push(geojsons[key]);
|
|
90
|
+ });
|
|
91
|
+ return layers;
|
|
92
|
+ },
|
|
93
|
+
|
|
94
|
+ hasLayerWithId: function (sublayer, id) {
|
|
95
|
+ if (!this._geojsons[sublayer] || !this._features[sublayer]) return false;
|
|
96
|
+ return this._features[sublayer].hasOwnProperty(id);
|
|
97
|
+ },
|
|
98
|
+
|
|
99
|
+ addData: function (data) {
|
|
100
|
+ if (data.type === 'FeatureCollection') {
|
|
101
|
+ this.addSubLayerData('default', data);
|
|
102
|
+ }
|
|
103
|
+ else {
|
|
104
|
+ var tileLayer = this;
|
|
105
|
+ Object.keys(data).forEach(function (key) {
|
|
106
|
+ tileLayer.addSubLayerData(key, data[key]);
|
|
107
|
+ });
|
|
108
|
+ }
|
|
109
|
+ },
|
|
110
|
+
|
|
111
|
+ addSubLayerData: function (sublayer, data) {
|
|
112
|
+ if (!this._geojsons[sublayer]) {
|
|
113
|
+ this._geojsons[sublayer] = new this.geoJsonClass(null, this.options.layers[sublayer]).addTo(this._map);
|
|
114
|
+ this.checkZoomConditions(this._map.getZoom());
|
|
115
|
+ }
|
|
116
|
+ var toAdd = data.features.filter(function (feature) {
|
|
117
|
+ return !this.hasLayerWithId(sublayer, feature.id ? feature.id : feature.properties.id);
|
|
118
|
+ }, this);
|
|
119
|
+
|
|
120
|
+ if (!this._features[sublayer]) {
|
|
121
|
+ this._features[sublayer] = {};
|
|
122
|
+ }
|
|
123
|
+ toAdd.forEach(function (feature) {
|
|
124
|
+ var id = feature.id ? feature.id : feature.properties.id;
|
|
125
|
+ this._features[sublayer][id] = feature;
|
|
126
|
+ }, this);
|
|
127
|
+
|
|
128
|
+ this._geojsons[sublayer].addData({
|
|
129
|
+ type: 'FeatureCollection',
|
|
130
|
+ features: toAdd
|
|
131
|
+ });
|
|
132
|
+ },
|
|
133
|
+
|
|
134
|
+ checkZoomConditions: function (zoom) {
|
|
135
|
+ var layers = this._geojsons,
|
|
136
|
+ map = this._map;
|
|
137
|
+ Object.keys(layers).forEach(function (key) {
|
|
138
|
+ var layer = layers[key],
|
|
139
|
+ options = layer.options;
|
|
140
|
+ if ((options.maxZoom && zoom > options.maxZoom) ||
|
|
141
|
+ (options.minZoom && zoom < options.minZoom)) {
|
|
142
|
+ map.removeLayer(layer);
|
|
143
|
+ }
|
|
144
|
+ else {
|
|
145
|
+ map.addLayer(layer);
|
|
146
|
+ }
|
|
147
|
+ });
|
|
148
|
+ }
|
|
149
|
+ });
|
|
150
|
+
|
|
151
|
+ L.geoJsonGridLayer = function(url, options) {
|
|
152
|
+ return new L.GeoJSONGridLayer(url, options);
|
|
153
|
+ };
|
|
154
|
+ }
|
|
155
|
+
|
|
156
|
+ if (typeof define === 'function' && define.amd) {
|
|
157
|
+ // Try to add leaflet.loading to Leaflet using AMD
|
|
158
|
+ define(['leaflet'], function (L) {
|
|
159
|
+ defineLeafletGeoJSONGridLayer(L);
|
|
160
|
+ });
|
|
161
|
+ }
|
|
162
|
+ else {
|
|
163
|
+ // Else use the global L
|
|
164
|
+ defineLeafletGeoJSONGridLayer(L);
|
|
165
|
+ }
|
|
166
|
+
|
|
167
|
+})();
|