Skip to content

Commit 9275ca6

Browse files
committed
support tiles in GeoJSON
1 parent 1e92edc commit 9275ca6

File tree

2 files changed

+53
-41
lines changed

2 files changed

+53
-41
lines changed

examples/clients/leaflet/geojson-layer.js

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,39 @@
77

88
url: null,
99
map: null,
10-
10+
11+
//
12+
// Leaflet layer methods
13+
//
1114
initialize(url, options) {
1215
this.url = url;
1316
L.GeoJSON.prototype.initialize.call(this, [], options);
1417
},
1518

16-
reloadMap: function() {
17-
if (this.map) {
18-
var url = this.expandUrl(this.url);
19-
this.ajaxRequest('GET', url, false, this.updateLayers.bind(this));
20-
}
21-
},
22-
23-
updateLayers: function(geoData) {
24-
this.clearLayers();
25-
this.addData(geoData);
26-
},
27-
2819
onAdd(map) {
2920
L.GeoJSON.prototype.onAdd.call(this, map);
3021
this.map = map;
31-
map.on('moveend zoomend refresh', this.reloadMap, this);
32-
this.reloadMap();
22+
map.on('moveend zoomend refresh', this._reloadMap, this);
23+
this._reloadMap();
3324
},
3425

3526
onRemove(map) {
36-
map.off('moveend zoomend refresh', this.reloadMap, this);
27+
map.off('moveend zoomend refresh', this._reloadMap, this);
3728
this.map = null;
3829
L.GeoJSON.prototype.onRemove.call(this, map);
3930
},
4031

41-
expandUrl: function(template) {
32+
//
33+
// Custom methods
34+
//
35+
_reloadMap: function() {
36+
if (this.map) {
37+
var url = this._expandUrl(this.url);
38+
this._ajaxRequest('GET', url, false, this._updateLayers.bind(this));
39+
}
40+
},
41+
42+
_expandUrl: function(template) {
4243
var bbox = this.map.getBounds();
4344
var southWest = bbox.getSouthWest();
4445
var northEast = bbox.getNorthEast();
@@ -52,8 +53,8 @@
5253
};
5354
return L.Util.template(template, coords);
5455
},
55-
56-
ajaxRequest: function(method, url, data, callback) {
56+
57+
_ajaxRequest: function(method, url, data, callback) {
5758
var request = new XMLHttpRequest();
5859
request.open(method, url, true);
5960
request.onreadystatechange = function() {
@@ -70,6 +71,11 @@
7071
return request;
7172
},
7273

74+
_updateLayers: function(geoData) {
75+
this.clearLayers();
76+
this.addData(geoData);
77+
}
78+
7379
});
7480

7581
L.geoJSONLayer = function (url, options) {

examples/clients/leaflet/geojson-tile-layer.js

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@
66
includes: L.Evented.prototype,
77

88
url: null,
9+
map: null,
910
layer: null,
1011
features: null,
1112
cache: null,
1213

14+
//
15+
// Leaflet layer methods
16+
//
1317
initialize(url, options) {
1418
this.url = url;
1519
this.layer = new L.GeoJSON(null, options);
@@ -23,14 +27,31 @@
2327
tile.style['box-shadow'] = 'inset 0 0 2px #f00';
2428
var url = L.Util.template(this.url, coords);
2529
if (this.cache[url]) {
26-
this.updateLayers(url, this.cache[url]);
30+
this._updateLayers(url, this.cache[url]);
2731
} else {
28-
this.ajaxRequest('GET', url, false, this.updateLayers.bind(this, url));
32+
this._ajaxRequest('GET', url, false, this._updateLayers.bind(this, url));
2933
}
3034
return tile;
3135
},
3236

33-
updateLayers: function(url, geoData) {
37+
onAdd(map) {
38+
L.GridLayer.prototype.onAdd.call(this, map);
39+
map.addLayer(this.layer);
40+
this.map = map;
41+
map.on('zoomanim', this._onZoomAnim.bind(this));
42+
},
43+
44+
onRemove(map) {
45+
map.off('zoomanim', this._onZoomAnim.bind(this));
46+
this.map = null;
47+
map.removeLayer(this.layer)
48+
L.GridLayer.prototype.onRemove.call(this, map);
49+
},
50+
51+
//
52+
// Custom methods
53+
//
54+
_updateLayers: function(url, geoData) {
3455
if (geoData.type == 'FeatureCollection'){
3556
geoData = geoData.features;
3657
}
@@ -46,21 +67,7 @@
4667
}
4768
},
4869

49-
onAdd(map) {
50-
L.GridLayer.prototype.onAdd.call(this, map);
51-
map.addLayer(this.layer);
52-
this.map = map;
53-
map.on('zoomanim', this.onZoomAnim.bind(this));
54-
},
55-
56-
onRemove(map) {
57-
map.off('zoomanim', this.onZoomAnim.bind(this));
58-
this.map = null;
59-
map.removeLayer(this.layer)
60-
L.GridLayer.prototype.onRemove.call(this, map);
61-
},
62-
63-
ajaxRequest: function(method, url, data, callback) {
70+
_ajaxRequest: function(method, url, data, callback) {
6471
var request = new XMLHttpRequest();
6572
request.open(method, url, true);
6673
request.onreadystatechange = function() {
@@ -77,20 +84,19 @@
7784
return request;
7885
},
7986

80-
onZoomAnim: function (e) {
87+
_onZoomAnim: function (e) {
8188
var zoom = e.zoom;
8289
if ((this.options.maxZoom && zoom > this.options.maxZoom) ||
8390
(this.options.minZoom && zoom < this.options.minZoom)) {
8491
this.map.removeLayer(this.layer);
8592
} else {
8693
this.map.addLayer(this.layer);
8794
}
88-
},
89-
95+
}
9096
});
9197

9298
L.geoJSONTileLayer = function (url, options) {
9399
return new L.GeoJSONTileLayer(url, options);
94100
};
95101

96-
}).call(this);
102+
})();

0 commit comments

Comments
 (0)