agenda/static/js/map.js
2024-01-24 12:03:56 +00:00

60 lines
2 KiB
JavaScript

if (![].at) {
Array.prototype.at = function(pos) { return this.slice(pos, pos + 1)[0] }
}
function emoji_icon(emoji) {
var iconStyle = "<div style='background-color: white; border-radius: 50%; width: 30px; height: 30px; display: flex; justify-content: center; align-items: center; border: 1px solid black;'> <div style='font-size: 18px;'>" + emoji + "</div></div>";
return L.divIcon({
className: 'custom-div-icon',
html: iconStyle,
iconSize: [60, 60],
iconAnchor: [15, 15],
});
}
var icons = {
"station": emoji_icon("🚉"),
"airport": emoji_icon("✈️"),
"accommodation": emoji_icon("🏨"),
"conference": emoji_icon("🎤"),
"event": 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 marker = L.marker([item.latitude, item.longitude], { icon: icons[item.type] }).addTo(map);
marker.bindPopup(item.name);
});
// Draw routes
routes.forEach(function(route) {
var color = {"train": "blue", "flight": "red", "unbooked_flight": "orange"}[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" || route.type === "unbooked_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;
}