agenda/static/js/map.js
2024-01-14 16:50:16 +00:00

49 lines
1.6 KiB
JavaScript

function emoji_icon(emoji) {
return L.divIcon({
className: 'custom-div-icon',
html: "<div style='font-size: 24px;'>" + emoji + "</div>",
iconSize: [30, 42],
});
}
var stationIcon = emoji_icon("🚉");
var airportIcon = emoji_icon("✈️");
function build_map(map_id, coordinates, routes) {
// Initialize the map
var map = L.map(map_id).fitBounds(coordinates.map(function(station) {
return [station.latitude, station.longitude];
}));
// Set up the tile layer
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
// Add markers with appropriate icons to the map
coordinates.forEach(function(item) {
var icon = item.type === "station" ? stationIcon : airportIcon;
var marker = L.marker([item.latitude, item.longitude], { icon: icon }).addTo(map);
marker.bindPopup(item.name);
});
// Draw routes
routes.forEach(function(route) {
var color = {"train": "blue", "flight": "red"}[route.type];
var style = { weight: 3, opacity: 0.5, color: color };
if (route.geojson) {
// If route is defined as GeoJSON
L.geoJSON(JSON.parse(route.geojson), {
style: function(feature) { return style; }
}).addTo(map);
} else if (route.type === "flight") {
var flightPath = new L.Geodesic([[route.from, route.to]], style).addTo(map);
} else {
// If route is defined by 'from' and 'to' coordinates
L.polyline([route.from, route.to], style).addTo(map);
}
});
return map;
}