maps: move geojson layer code to its own method (#17082)

This commit is contained in:
Frédéric Péters 2017-06-21 17:28:32 +02:00
parent e558f02b6f
commit 3437832be3
1 changed files with 43 additions and 25 deletions

View File

@ -1,4 +1,39 @@
$(function() {
L.Map.include(
{
add_geojson_layer: function(callback) {
var map = this;
var $map_widget = $(map.getContainer());
var cell = $map_widget.parents('div.cell')[0];
var geojson_url = $map_widget.data('geojson-url');
if (!geojson_url) return;
$.getJSON(geojson_url, function(data) {
var geo_json = L.geoJson(data, {
onEachFeature: function(feature, layer) {
$(cell).trigger('combo:map-feature-prepare', {'feature': feature, 'layer': layer});
},
pointToLayer: function (feature, latlng) {
var markerStyles = "background-color: " + feature.properties.layer.colour + ";";
marker = L.divIcon({iconAnchor: [0, 30],
popupAnchor: [5, -45],
html: '<span style="' + markerStyles + '"><i class="leaflet-marker-icon ' +
feature.properties.layer.icon + '" style="color:' +
feature.properties.layer.icon_colour +'"></i></span>'
});
return L.marker(latlng, {icon: marker});
}
});
if (map.geo_json) map.geo_json.remove();
map.geo_json = geo_json;
geo_json.addTo(map);
if (callback) {
callback(geo_json);
}
});
}
});
function render_map(cell) {
var $map_widget = $(cell).find('div.combo-cell-map');
var map_options = Object();
@ -24,6 +59,7 @@ $(function() {
var map = L.map($map_widget[0], map_options);
var store_position_selector = $map_widget.data('store-position');
$map_widget[0].leaflet_map = map;
map.setView(latlng, map_options.zoom);
if (init_state == 'device-location') {
@ -38,32 +74,14 @@ $(function() {
attribution: map_attribution,
maxZoom: map_options.maxZoom
}).addTo(map);
if (geojson_url) {
$.getJSON(geojson_url, function(data) {
var geo_json = L.geoJson(data, {
onEachFeature: function(feature, layer) {
$(cell).trigger('combo:map-feature-prepare', {'feature': feature, 'layer': layer});
},
pointToLayer: function (feature, latlng) {
var markerStyles = "background-color: " + feature.properties.layer.colour + ";";
marker = L.divIcon({iconAnchor: [0, 30],
popupAnchor: [5, -45],
html: '<span style="' + markerStyles + '"><i class="leaflet-marker-icon ' +
feature.properties.layer.icon + '" style="color:' +
feature.properties.layer.icon_colour +'"></i></span>'
});
return L.marker(latlng, {icon: marker});
}
});
var bounds = geo_json.getBounds();
if (bounds.isValid()) {
if (init_state == 'fit-markers') {
map.fitBounds(bounds);
}
geo_json.addTo(map);
map.add_geojson_layer(function(geo_json) {
var bounds = geo_json.getBounds();
if (bounds.isValid()) {
if (init_state == 'fit-markers') {
map.fitBounds(bounds);
}
});
}
}
});
};
$('div.cell.map').each(function() {
render_map(this);