Skip to content

Commit 2105d58

Browse files
committed
support tiles in GeoJSON
1 parent bf3bf10 commit 2105d58

File tree

1 file changed

+68
-16
lines changed

1 file changed

+68
-16
lines changed

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

Lines changed: 68 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
layer: null,
1111
features: null,
1212
cache: null,
13-
13+
1414
//
1515
// Leaflet layer methods
1616
//
@@ -21,33 +21,39 @@
2121
this.cache = {};
2222
L.GridLayer.prototype.initialize.call(this, options);
2323
},
24-
25-
createTile: function (coords) {
24+
25+
createTile(coords, done) {
2626
var tile = L.DomUtil.create('div', 'leaflet-tile');
2727
tile.style['box-shadow'] = 'inset 0 0 2px #f00';
2828
var url = this._expandUrl(this.url, coords);
29-
if (this.cache[url]) {
30-
this._updateLayers(url, this.cache[url]);
29+
if (this.cache[coords]) {
30+
done.call(this);
3131
} else {
32-
this._ajaxRequest('GET', url, false, this._updateLayers.bind(this, url));
32+
this._ajaxRequest('GET', url, false, this._updateCache.bind(this, done, coords));
3333
}
3434
return tile;
3535
},
36-
36+
3737
onAdd(map) {
3838
L.GridLayer.prototype.onAdd.call(this, map);
3939
map.addLayer(this.layer);
4040
this.map = map;
4141
map.on('zoomanim', this._onZoomAnim.bind(this));
42+
this.on('loading', this._onLoading.bind(this));
43+
this.on('tileload', this._onTileLoad.bind(this));
44+
this.on('tileunload', this._onTileUnLoad.bind(this));
4245
},
43-
46+
4447
onRemove(map) {
48+
this.off('tileunload', this._onTileUnLoad.bind(this));
49+
this.off('tileload', this._onTileLoad.bind(this));
50+
this.off('loading', this._onLoading.bind(this));
4551
map.off('zoomanim', this._onZoomAnim.bind(this));
4652
this.map = null;
4753
map.removeLayer(this.layer)
4854
L.GridLayer.prototype.onRemove.call(this, map);
4955
},
50-
56+
5157
//
5258
// Custom methods
5359
//
@@ -80,23 +86,53 @@
8086
});
8187
return L.Util.template(template, coords);
8288
},
83-
84-
_updateLayers: function(url, geoData) {
89+
90+
_hashCode: function(str) {
91+
var hash = 0, i, chr;
92+
if (str.length === 0) return hash;
93+
for (i = 0; i < str.length; i++) {
94+
chr = str.charCodeAt(i);
95+
hash = ((hash << 5) - hash) + chr;
96+
hash |= 0; // Convert to 32bit integer
97+
}
98+
return hash;
99+
},
100+
101+
_updateTiles: function() {
102+
this.layer.clearLayers();
103+
this.features = {};
104+
for (var coords in this.cache) {
105+
if (this.cache.hasOwnProperty(coords)) {
106+
this._drawTile(coords);
107+
}
108+
}
109+
},
110+
111+
_drawTile(coords) {
112+
var geoData = this.cache[coords];
85113
if (geoData.type == 'FeatureCollection'){
86114
geoData = geoData.features;
87115
}
88116
for (var i=0;i<geoData.length;i++) {
117+
if (!geoData[i].id) {
118+
geoData[i].id = this._hashCode(JSON.stringify(geoData[i].geometry));
119+
}
89120
var id = geoData[i].id;
90121
if (!this.features[id]) {
91122
this.layer.addData(geoData[i]);
92123
this.features[id] = true;
93124
}
94125
}
95-
if (!this.cache[url]) {
96-
this.cache[url] = geoData;
126+
if (!this.cache[coords]) {
127+
this.cache[coords] = geoData;
97128
}
98129
},
99-
130+
131+
_updateCache: function(done, coords, geoData) {
132+
this.cache[coords] = geoData;
133+
done.call(this);
134+
},
135+
100136
_ajaxRequest: function(method, url, data, callback) {
101137
var request = new XMLHttpRequest();
102138
request.open(method, url, true);
@@ -113,16 +149,32 @@
113149
}
114150
return request;
115151
},
116-
152+
117153
_onZoomAnim: function (e) {
118154
var zoom = e.zoom;
119155
if ((this.options.maxZoom && zoom > this.options.maxZoom) ||
120156
(this.options.minZoom && zoom < this.options.minZoom)) {
121157
this.map.removeLayer(this.layer);
158+
this.cache = {};
159+
this.layer.clearLayers();
122160
} else {
161+
this._updateTiles();
123162
this.map.addLayer(this.layer);
124163
}
125-
}
164+
},
165+
166+
_onLoading: function (e) {
167+
this._updateTiles();
168+
},
169+
170+
_onTileLoad: function (e) {
171+
this._drawTile(e.coords);
172+
},
173+
174+
_onTileUnLoad: function (e) {
175+
delete this.cache[e.coords]
176+
},
177+
126178
});
127179

128180
L.geoJSONTileLayer = function (url, options) {

0 commit comments

Comments
 (0)