Show station and airport icons on the map

This commit is contained in:
Edward Betts 2024-01-12 16:54:52 +00:00
parent 0c02d9c899
commit 4b8b1f7556
2 changed files with 45 additions and 17 deletions

View file

@ -6,7 +6,7 @@
{% block style %}
{% if station_coordinates %}
{% if coordinates %}
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css"
integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY="
crossorigin=""/>
@ -77,7 +77,7 @@
{% endfor %}
</div>
{% if station_coordinates %}
{% if coordinates %}
<div id="map"></div>
{% endif %}
@ -86,25 +86,41 @@
{% block scripts %}
{% if station_coordinates %}
{% if coordinates %}
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"
integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo="
crossorigin=""></script>
<script>
var station_coordinates = {{ station_coordinates | tojson }};
var coordinates = {{ coordinates | tojson }};
// Initialize the map
var map = L.map('map').fitBounds(station_coordinates);
var map = L.map('map').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 to the map
station_coordinates.forEach(function(coord) {
L.marker(coord).addTo(map);
var stationIcon = L.divIcon({
className: 'custom-div-icon',
html: "<div style='font-size: 24px;'>🚉</div>",
iconSize: [30, 42],
});
var airportIcon = L.divIcon({
className: 'custom-div-icon',
html: "<div style='font-size: 24px;'>✈️</div>",
iconSize: [30, 42],
});
// 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);
});
</script>