Browse Source

support tiles in GeoJSON

Maurits van der Schee 5 years ago
parent
commit
9275ca612c

+ 25
- 19
examples/clients/leaflet/geojson-layer.js View File

@@ -7,38 +7,39 @@
7 7
 
8 8
         url: null,
9 9
         map: null,
10
-
10
+        
11
+        //
12
+        // Leaflet layer methods
13
+        //
11 14
         initialize(url, options) {
12 15
             this.url = url;
13 16
             L.GeoJSON.prototype.initialize.call(this, [], options);
14 17
         },
15 18
 
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
-
28 19
         onAdd(map) {
29 20
             L.GeoJSON.prototype.onAdd.call(this, map); 
30 21
             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();
33 24
         },
34 25
 
35 26
         onRemove(map) {
36
-            map.off('moveend zoomend refresh', this.reloadMap, this);
27
+            map.off('moveend zoomend refresh', this._reloadMap, this);
37 28
             this.map = null;
38 29
             L.GeoJSON.prototype.onRemove.call(this, map);
39 30
         },
40 31
 
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) {
42 43
             var bbox = this.map.getBounds();
43 44
             var southWest = bbox.getSouthWest();
44 45
             var northEast = bbox.getNorthEast();
@@ -52,8 +53,8 @@
52 53
             };
53 54
             return L.Util.template(template, coords);
54 55
         },
55
-
56
-        ajaxRequest: function(method, url, data, callback) {
56
+        
57
+        _ajaxRequest: function(method, url, data, callback) {
57 58
             var request = new XMLHttpRequest();
58 59
             request.open(method, url, true);
59 60
             request.onreadystatechange = function() {
@@ -70,6 +71,11 @@
70 71
             return request;
71 72
         },
72 73
 
74
+        _updateLayers: function(geoData) {
75
+            this.clearLayers();
76
+            this.addData(geoData);
77
+        }
78
+
73 79
     });
74 80
 
75 81
     L.geoJSONLayer = function (url, options) {

+ 28
- 22
examples/clients/leaflet/geojson-tile-layer.js View File

@@ -6,10 +6,14 @@
6 6
         includes: L.Evented.prototype,
7 7
 
8 8
         url: null,
9
+        map: null,
9 10
         layer: null,
10 11
         features: null,
11 12
         cache: null,
12 13
 
14
+        //
15
+        // Leaflet layer methods
16
+        //
13 17
         initialize(url, options) {
14 18
             this.url = url;
15 19
             this.layer = new L.GeoJSON(null, options);
@@ -23,14 +27,31 @@
23 27
             tile.style['box-shadow'] = 'inset 0 0 2px #f00';
24 28
             var url = L.Util.template(this.url, coords);
25 29
             if (this.cache[url]) {
26
-                this.updateLayers(url, this.cache[url]);
30
+                this._updateLayers(url, this.cache[url]);
27 31
             } else {
28
-                this.ajaxRequest('GET', url, false, this.updateLayers.bind(this, url));
32
+                this._ajaxRequest('GET', url, false, this._updateLayers.bind(this, url));
29 33
             }
30 34
             return tile;
31 35
         },
32 36
 
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) {
34 55
             if (geoData.type == 'FeatureCollection'){
35 56
                 geoData = geoData.features;
36 57
             }
@@ -46,21 +67,7 @@
46 67
             }
47 68
         },
48 69
 
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) {
64 71
             var request = new XMLHttpRequest();
65 72
             request.open(method, url, true);
66 73
             request.onreadystatechange = function() {
@@ -77,7 +84,7 @@
77 84
             return request;
78 85
         },
79 86
 
80
-        onZoomAnim: function (e) {
87
+        _onZoomAnim: function (e) {
81 88
             var zoom = e.zoom;
82 89
             if ((this.options.maxZoom && zoom > this.options.maxZoom) ||
83 90
                 (this.options.minZoom && zoom < this.options.minZoom)) {
@@ -85,12 +92,11 @@
85 92
             } else {
86 93
                 this.map.addLayer(this.layer);
87 94
             }
88
-        },
89
-
95
+        }
90 96
     });
91 97
 
92 98
     L.geoJSONTileLayer = function (url, options) {
93 99
         return new L.GeoJSONTileLayer(url, options);
94 100
     };
95 101
 
96
-}).call(this);
102
+})();

Loading…
Cancel
Save