2024-01-16 09:00:03 +00:00
|
|
|
if (![].at) {
|
|
|
|
Array.prototype.at = function(pos) { return this.slice(pos, pos + 1)[0] }
|
|
|
|
}
|
|
|
|
|
2024-01-14 12:01:33 +00:00
|
|
|
function emoji_icon(emoji) {
|
2024-01-14 21:28:56 +00:00
|
|
|
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>";
|
|
|
|
|
2024-01-14 12:01:33 +00:00
|
|
|
return L.divIcon({
|
|
|
|
className: 'custom-div-icon',
|
2024-01-14 21:28:56 +00:00
|
|
|
html: iconStyle,
|
|
|
|
iconSize: [60, 60],
|
|
|
|
iconAnchor: [15, 15],
|
2024-01-14 12:01:33 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-01-14 21:00:19 +00:00
|
|
|
var icons = {
|
|
|
|
"station": emoji_icon("🚉"),
|
|
|
|
"airport": emoji_icon("✈️"),
|
2024-05-01 13:00:24 +01:00
|
|
|
"ferry_terminal": emoji_icon("🚢"),
|
2024-01-14 21:00:19 +00:00
|
|
|
"accommodation": emoji_icon("🏨"),
|
2024-01-14 21:43:10 +00:00
|
|
|
"conference": emoji_icon("🎤"),
|
2024-01-24 12:03:56 +00:00
|
|
|
"event": emoji_icon("🍷"),
|
2024-01-14 21:00:19 +00:00
|
|
|
}
|
2024-01-14 12:01:33 +00:00
|
|
|
|
2024-05-06 09:36:00 +01:00
|
|
|
|
2024-01-14 12:01:33 +00:00
|
|
|
function build_map(map_id, coordinates, routes) {
|
2024-05-06 09:36:00 +01:00
|
|
|
var map = L.map(map_id).fitBounds(coordinates.map(station => [station.latitude, station.longitude]));
|
|
|
|
|
|
|
|
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
|
|
|
|
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
|
|
|
|
}).addTo(map);
|
|
|
|
|
|
|
|
var markers = [];
|
|
|
|
var offset_lines = [];
|
2024-01-14 12:01:33 +00:00
|
|
|
|
2024-05-06 09:36:00 +01:00
|
|
|
function getIconBounds(latlng) {
|
2024-05-06 10:48:06 +01:00
|
|
|
let iconSize = 20; // Assuming the icon size as a square
|
2024-05-06 09:36:00 +01:00
|
|
|
if (!latlng) return null;
|
|
|
|
let pixel = map.project(latlng, map.getZoom());
|
|
|
|
let sw = map.unproject([pixel.x - iconSize / 2, pixel.y + iconSize / 2], map.getZoom());
|
|
|
|
let ne = map.unproject([pixel.x + iconSize / 2, pixel.y - iconSize / 2], map.getZoom());
|
|
|
|
return L.latLngBounds(sw, ne);
|
2024-01-14 12:01:33 +00:00
|
|
|
}
|
|
|
|
|
2024-05-06 09:36:00 +01:00
|
|
|
function calculateCentroid(markers) {
|
|
|
|
let latSum = 0, lngSum = 0, count = 0;
|
|
|
|
markers.forEach(marker => {
|
|
|
|
latSum += marker.getLatLng().lat;
|
|
|
|
lngSum += marker.getLatLng().lng;
|
|
|
|
count++;
|
|
|
|
});
|
|
|
|
return count > 0 ? L.latLng(latSum / count, lngSum / count) : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Function to detect and group overlapping markers
|
|
|
|
function getOverlappingGroups() {
|
|
|
|
let groups = [];
|
|
|
|
let visited = new Set();
|
|
|
|
|
|
|
|
markers.forEach((marker, index) => {
|
|
|
|
if (visited.has(marker)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let group = [];
|
|
|
|
let markerBounds = getIconBounds(marker.getLatLng());
|
|
|
|
|
|
|
|
markers.forEach((otherMarker) => {
|
|
|
|
if (marker !== otherMarker && markerBounds.intersects(getIconBounds(otherMarker.getLatLng()))) {
|
|
|
|
group.push(otherMarker);
|
|
|
|
visited.add(otherMarker);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (group.length > 0) {
|
|
|
|
group.push(marker); // Add the original marker to the group
|
|
|
|
groups.push(group);
|
|
|
|
visited.add(marker);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return groups;
|
|
|
|
}
|
|
|
|
|
|
|
|
function displaceMarkers(group, zoom) {
|
|
|
|
const markerPixelSize = 30; // Width/height of the marker in pixels
|
|
|
|
let map = group[0]._map; // Assuming all markers are on the same map
|
|
|
|
|
|
|
|
let centroid = calculateCentroid(group);
|
|
|
|
let centroidPoint = map.project(centroid, zoom);
|
|
|
|
|
|
|
|
const radius = markerPixelSize; // Set radius for even distribution
|
|
|
|
const angleIncrement = (2 * Math.PI) / group.length; // Evenly space markers
|
|
|
|
|
|
|
|
group.forEach((marker, index) => {
|
|
|
|
let angle = index * angleIncrement;
|
|
|
|
let newX = centroidPoint.x + radius * Math.cos(angle);
|
|
|
|
let newY = centroidPoint.y + radius * Math.sin(angle);
|
|
|
|
let newPoint = L.point(newX, newY);
|
|
|
|
let newLatLng = map.unproject(newPoint, zoom);
|
|
|
|
|
|
|
|
// Store original position for polyline
|
|
|
|
let originalPos = marker.getLatLng();
|
|
|
|
marker.setLatLng(newLatLng);
|
|
|
|
|
|
|
|
marker.polyline = L.polyline([originalPos, newLatLng], {color: "gray", weight: 2}).addTo(map);
|
|
|
|
offset_lines.push(marker.polyline);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
coordinates.forEach(function(item, index) {
|
|
|
|
let latlng = L.latLng(item.latitude, item.longitude);
|
|
|
|
let marker = L.marker(latlng, { icon: icons[item.type] }).addTo(map);
|
|
|
|
marker.bindPopup(item.name);
|
|
|
|
markers.push(marker);
|
|
|
|
});
|
|
|
|
|
|
|
|
map.on('zoomend', function() {
|
|
|
|
markers.forEach((marker, index) => {
|
|
|
|
marker.setLatLng([coordinates[index].latitude, coordinates[index].longitude]); // Reset position on zoom
|
|
|
|
if (marker.polyline) {
|
|
|
|
map.removeLayer(marker.polyline);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
offset_lines.forEach(polyline => {
|
|
|
|
map.removeLayer(polyline);
|
|
|
|
});
|
|
|
|
|
|
|
|
let overlappingGroups = getOverlappingGroups();
|
|
|
|
// console.log(overlappingGroups); // Process or display groups as needed
|
|
|
|
|
|
|
|
overlappingGroups.forEach(group => displaceMarkers(group, map.getZoom()));
|
|
|
|
});
|
|
|
|
|
|
|
|
let overlappingGroups = getOverlappingGroups();
|
|
|
|
// console.log(overlappingGroups); // Process or display groups as needed
|
|
|
|
|
|
|
|
overlappingGroups.forEach(group => displaceMarkers(group, map.getZoom()));
|
|
|
|
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
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 {
|
|
|
|
L.polyline([route.from, route.to], style).addTo(map);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return map;
|
2024-01-14 12:01:33 +00:00
|
|
|
}
|