|
@@ -0,0 +1,81 @@
|
|
1
|
+/* global L */
|
|
2
|
+(function() {
|
|
3
|
+
|
|
4
|
+ L.GeoJSONURL = L.GeoJSON.extend({
|
|
5
|
+
|
|
6
|
+ includes: L.Evented.prototype,
|
|
7
|
+
|
|
8
|
+ map: null,
|
|
9
|
+
|
|
10
|
+ options: {
|
|
11
|
+ },
|
|
12
|
+
|
|
13
|
+ initialize(extraOptions, options) {
|
|
14
|
+ L.GeoJSON.prototype.initialize.call(this, [], options);
|
|
15
|
+ L.Util.setOptions(this, extraOptions);
|
|
16
|
+ },
|
|
17
|
+
|
|
18
|
+ _reload: function() {
|
|
19
|
+ if (this.map) {
|
|
20
|
+ var url = this._expand(this.options.url);
|
|
21
|
+ this._ajax('GET', url, false, this._update.bind(this));
|
|
22
|
+ }
|
|
23
|
+ },
|
|
24
|
+
|
|
25
|
+ _update: function(geoData) {
|
|
26
|
+ this.clearLayers();
|
|
27
|
+ this.addData(geoData);
|
|
28
|
+ },
|
|
29
|
+
|
|
30
|
+ onAdd: function(map) {
|
|
31
|
+ L.GeoJSON.prototype.onAdd.call(this, map);
|
|
32
|
+ this.map = map;
|
|
33
|
+ map.on('moveend zoomend refresh', this._reload, this);
|
|
34
|
+ this._reload();
|
|
35
|
+ },
|
|
36
|
+
|
|
37
|
+ onRemove: function(map) {
|
|
38
|
+ map.off('moveend zoomend refresh', this._reload, this);
|
|
39
|
+ this.map = null;
|
|
40
|
+ L.GeoJSON.prototype.onRemove.call(this, map);
|
|
41
|
+ },
|
|
42
|
+
|
|
43
|
+ _expand: function(template) {
|
|
44
|
+ var bbox = this._map.getBounds();
|
|
45
|
+ var southWest = bbox.getSouthWest();
|
|
46
|
+ var northEast = bbox.getNorthEast();
|
|
47
|
+ var bboxStr = bbox.toBBoxString();
|
|
48
|
+ var coords = {
|
|
49
|
+ lat1: southWest.lat,
|
|
50
|
+ lon1: southWest.lng,
|
|
51
|
+ lat2: northEast.lat,
|
|
52
|
+ lon2: northEast.lng,
|
|
53
|
+ bbox: bboxStr
|
|
54
|
+ };
|
|
55
|
+ return L.Util.template(template, coords);
|
|
56
|
+ },
|
|
57
|
+
|
|
58
|
+ _ajax: function(method, url, data, callback) {
|
|
59
|
+ var request = new XMLHttpRequest();
|
|
60
|
+ request.open(method, url, true);
|
|
61
|
+ request.onreadystatechange = function() {
|
|
62
|
+ if (request.readyState === 4 && request.status === 200) {
|
|
63
|
+ callback(JSON.parse(request.responseText));
|
|
64
|
+ }
|
|
65
|
+ };
|
|
66
|
+ if (data) {
|
|
67
|
+ request.setRequestHeader('Content-type', 'application/json');
|
|
68
|
+ request.send(JSON.stringify(data));
|
|
69
|
+ } else {
|
|
70
|
+ request.send();
|
|
71
|
+ }
|
|
72
|
+ return request;
|
|
73
|
+ },
|
|
74
|
+
|
|
75
|
+});
|
|
76
|
+
|
|
77
|
+L.geoJSONURL = function (options) {
|
|
78
|
+ return new L.GeoJSONURL(options);
|
|
79
|
+};
|
|
80
|
+
|
|
81
|
+}).call(this);
|