1 |
1 |
$(function() {
|
2 |
2 |
L.Map.include(
|
3 |
3 |
{
|
4 |
|
add_geojson_layer: function(callback, geojson_data) {
|
|
4 |
init_geojson_layers: function(layers) {
|
|
5 |
if (Array.isArray(layers)) { // backward compatibility
|
|
6 |
this.geojson_layers = Object();
|
|
7 |
for (var i=0; i<layers.length; i++) {
|
|
8 |
this.geojson_layers[layers[i].slug] = Object();
|
|
9 |
this.geojson_layers[layers[i].slug].url = layers[i].url;
|
|
10 |
}
|
|
11 |
} else {
|
|
12 |
this.geojson_layers = layers;
|
|
13 |
}
|
|
14 |
},
|
|
15 |
|
|
16 |
load_geojson_layers: function(callback) {
|
|
17 |
var layer_slugs = Object.keys(this.geojson_layers);
|
|
18 |
for (var i=0; i<layer_slugs.length; i++) {
|
|
19 |
var layer = this.geojson_layers[layer_slugs[i]];
|
|
20 |
layer.status = 'loading';
|
|
21 |
this.add_geojson_layer(callback, layer);
|
|
22 |
}
|
|
23 |
},
|
|
24 |
|
|
25 |
add_geojson_layer: function(callback, geojson_layer) {
|
5 |
26 |
var map = this;
|
6 |
27 |
var $map_widget = $(map.getContainer());
|
7 |
28 |
var cell = $map_widget.parents('div.cell')[0];
|
8 |
|
var geojson_url = geojson_data.url;
|
9 |
29 |
|
10 |
|
$.getJSON(geojson_url).done(
|
|
30 |
$.getJSON(geojson_layer.url).done(
|
11 |
31 |
function(data) {
|
12 |
32 |
var geo_json = L.geoJson(data, {
|
13 |
33 |
onEachFeature: function(feature, layer) {
|
... | ... | |
59 |
79 |
return L.marker(latlng, {icon: marker});
|
60 |
80 |
}
|
61 |
81 |
});
|
|
82 |
|
|
83 |
// track global marker bounds
|
|
84 |
var bounds = geo_json.getBounds();
|
|
85 |
if (bounds.isValid()) {
|
|
86 |
if (!map.bounds) {
|
|
87 |
map.bounds = L.latLngBounds(bounds);
|
|
88 |
} else {
|
|
89 |
map.bounds.extend(bounds);
|
|
90 |
}
|
|
91 |
}
|
|
92 |
|
62 |
93 |
if ($map_widget.data('group-markers')) {
|
63 |
|
if (map.geojson_layers[geojson_data.slug]) {
|
64 |
|
map.clustered_markers.removeLayer(map.geojson_layers[geojson_data.slug]);
|
|
94 |
if (geojson_layer.leaflet_layer) {
|
|
95 |
map.clustered_markers.removeLayer(geojson_layer.leaflet_layer);
|
65 |
96 |
}
|
66 |
97 |
map.clustered_markers.addLayer(geo_json);
|
67 |
98 |
} else {
|
68 |
|
if (map.geojson_layers[geojson_data.slug]) {
|
69 |
|
map.geojson_layers[geojson_data.slug].remove();
|
|
99 |
if (geojson_layer.leaflet_layer) {
|
|
100 |
geojson_layer.leaflet_layer.remove();
|
70 |
101 |
}
|
71 |
102 |
geo_json.addTo(map);
|
72 |
103 |
}
|
73 |
|
map.geojson_layers[geojson_data.slug] = geo_json;
|
|
104 |
geojson_layer.leaflet_layer = geo_json;
|
74 |
105 |
if (callback) {
|
75 |
106 |
callback(geo_json);
|
76 |
107 |
}
|
77 |
|
geojson_data.status = 'loaded';
|
|
108 |
geojson_layer.status = 'loaded';
|
78 |
109 |
}).fail(function(jqxhr, textStatus, error) {
|
79 |
|
geojson_data.status = 'error';
|
|
110 |
geojson_layer.status = 'error';
|
80 |
111 |
}).always(function() {
|
81 |
112 |
map.trigger_markers_if_ready();
|
82 |
113 |
});
|
83 |
114 |
},
|
|
115 |
|
84 |
116 |
trigger_markers_if_ready: function() {
|
85 |
117 |
var map = this;
|
86 |
118 |
var $map_widget = $(map.getContainer());
|
... | ... | |
153 |
185 |
});
|
154 |
186 |
map.addLayer(map.clustered_markers);
|
155 |
187 |
}
|
156 |
|
map.geojson_layers = Object();
|
157 |
188 |
if ($map_widget.data('draggable') === false) {
|
158 |
189 |
map.dragging.disable();
|
159 |
190 |
}
|
... | ... | |
209 |
240 |
).addTo(map);
|
210 |
241 |
});
|
211 |
242 |
map.bounds = null;
|
212 |
|
map.geojson_layers_loaded = 0;
|
213 |
|
var geojson_layers = window['geojson_'+map_id];
|
214 |
|
if (geojson_layers.length) {
|
215 |
|
$.each(geojson_layers, function(idx, geojson_data) {
|
216 |
|
geojson_data.status = 'loading';
|
217 |
|
map.add_geojson_layer(function(geo_json) {
|
218 |
|
var bounds = geo_json.getBounds();
|
219 |
|
if (bounds.isValid()) {
|
220 |
|
if (!map.bounds) {
|
221 |
|
map.bounds = L.latLngBounds(bounds);
|
222 |
|
} else {
|
223 |
|
map.bounds.extend(bounds);
|
224 |
|
}
|
225 |
|
if (init_state == 'fit-markers') {
|
226 |
|
map.fitBounds(map.bounds);
|
227 |
|
}
|
228 |
|
}
|
229 |
|
}, geojson_data);
|
230 |
|
map.geojson_layers_loaded += 1;
|
231 |
|
if (map.geojson_layers_loaded == geojson_layers.length) {
|
232 |
|
$(cell).trigger('combo:map-ready');
|
233 |
|
}
|
234 |
|
});
|
235 |
|
} else {
|
236 |
|
$(cell).trigger('combo:map-ready');
|
|
243 |
map.init_geojson_layers(window['geojson_'+map_id]);
|
|
244 |
if (init_state == 'fit-markers') {
|
|
245 |
$(cell).on('combo:map-markers-ready', function() {
|
|
246 |
map.fitBounds(map.bounds);
|
|
247 |
});
|
237 |
248 |
}
|
|
249 |
map.load_geojson_layers()
|
|
250 |
$(cell).trigger('combo:map-ready');
|
238 |
251 |
};
|
239 |
252 |
$('div.cell.map').each(function() {
|
240 |
253 |
render_map(this);
|