caprigis/js/caprigis.js
2024-03-13 16:34:50 +01:00

53 lines
1.3 KiB
JavaScript

'use strict';
// Global leaflet
/**
* @namespace GIS
*/
const GIS = {};
const BASE_URL = location.href;
/**
* @param {string} mapId
* @param {number} zoomLevel
* @returns {Map}
*/
GIS.initMap = function (mapId, zoomLevel = 15) {
let map = L.map(mapId, {
attributionControl: false,
minZoom: 3
}).setView([40.5492, 14.2317], zoomLevel);
// Il sistema di riferimento per i livelli geoJSON è EPSG3857
map.crs = L.CRS.EPSG3857;
L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
return map;
}
/**
* @param {string} geoJSON
* @param {Map} map
*/
GIS.loadLayer = async function (geoJSON, map, color = '#555') {
const layer = await fetch(`${BASE_URL}/geojson/${geoJSON}`)
.then(res => res.json())
.catch(error => console.error(`Can't load layer ${geoJSON}. Reason: ${error}`));
L.geoJson(layer, {
style: function (feature) {
let style = {
color: color,
opacity: 0.4,
weight: 2,
fillColor: color,
fillOpacity: 1
};
return style;
}
}).addTo(map);
}
export default GIS;