107 lines
4.5 KiB
JavaScript
107 lines
4.5 KiB
JavaScript
$(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, 0],
|
|
popupAnchor: [5, -45],
|
|
html: '<span class="layer-' +
|
|
feature.properties.layer.identifier +
|
|
'" 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);
|
|
}
|
|
$(cell).trigger('combo:map-markers-ready');
|
|
});
|
|
}
|
|
});
|
|
|
|
function render_map(cell) {
|
|
var $map_widget = $(cell).find('div.combo-cell-map');
|
|
var map_options = Object();
|
|
var initial_zoom = parseInt($map_widget.data('init-zoom'));
|
|
if (! isNaN(initial_zoom)) {
|
|
map_options.zoom = initial_zoom;
|
|
} else {
|
|
map_options.zoom = 13;
|
|
}
|
|
var init_state = $map_widget.data('init-state');
|
|
var max_zoom = parseInt($map_widget.data('max-zoom'));
|
|
if (!isNaN(max_zoom)) {
|
|
map_options.maxZoom = max_zoom;
|
|
} else {
|
|
map_options.maxZoom = 19;
|
|
}
|
|
var min_zoom = parseInt($map_widget.data('min-zoom'));
|
|
if (!isNaN(min_zoom)) map_options.minZoom = min_zoom;
|
|
map_options.zoomControl = false;
|
|
var latlng = [$map_widget.data('init-lat'), $map_widget.data('init-lng')];
|
|
var geojson_url = $map_widget.data('geojson-url');
|
|
var map_tile_url = $map_widget.data('tile-urltemplate');
|
|
var map_attribution = $map_widget.data('map-attribution');
|
|
if ($map_widget.data('max-bounds-lat1')) {
|
|
map_options.maxBounds = L.latLngBounds(
|
|
L.latLng($map_widget.data('max-bounds-lat1'), $map_widget.data('max-bounds-lng1')),
|
|
L.latLng($map_widget.data('max-bounds-lat2'), $map_widget.data('max-bounds-lng2')))
|
|
}
|
|
var map = L.map($map_widget[0], map_options);
|
|
var map_controls_position = $('body').data('map-controls-position') || 'topleft';
|
|
new L.Control.Zoom({position: map_controls_position}).addTo(map);
|
|
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') {
|
|
map.locate({timeout: 10000, maximumAge: 300000, enableHighAccuracy: false});
|
|
map.on('locationfound', function(e) {
|
|
if (map_options.maxBounds && ! map_options.maxBounds.contains(e.latlng)) {
|
|
/* ouf of bounds, keep map centered on default position */
|
|
return;
|
|
}
|
|
map.setView(e.latlng, map_options.zoom);
|
|
});
|
|
}
|
|
|
|
L.tileLayer(map_tile_url,
|
|
{
|
|
attribution: map_attribution,
|
|
maxZoom: map_options.maxZoom
|
|
}).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);
|
|
}
|
|
}
|
|
$(cell).trigger('combo:map-ready');
|
|
});
|
|
};
|
|
$('div.cell.map').each(function() {
|
|
render_map(this);
|
|
});
|
|
});
|