From bd61b1bccd3239253b72d302b8b685eb26db2540 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Sun, 14 Jan 2024 12:01:33 +0000 Subject: [PATCH] Move map code into dedicated JS file --- static/js/map.js | 53 ++++++++++++++++++++++++++++++++++++++++ templates/trip_list.html | 48 +++--------------------------------- templates/trip_page.html | 50 ++++--------------------------------- 3 files changed, 62 insertions(+), 89 deletions(-) create mode 100644 static/js/map.js diff --git a/static/js/map.js b/static/js/map.js new file mode 100644 index 0000000..3bd6a42 --- /dev/null +++ b/static/js/map.js @@ -0,0 +1,53 @@ +function emoji_icon(emoji) { + return L.divIcon({ + className: 'custom-div-icon', + html: "
🚉
", + 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: '© OpenStreetMap 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) { + if (route.geojson) { + // If route is defined as GeoJSON + L.geoJSON(JSON.parse(route.geojson), { + style: function(feature) { + return {color: route.type === "train" ? "blue" : "blue"}; // Green for trains, blue for flights + } + }).addTo(map); + } else if (route.type === "flight") { + var flightPath = new L.Geodesic([[route.from, route.to]], { + weight: 3, + opacity: 0.5, + color: 'red' + }).addTo(map); + } else { + // If route is defined by 'from' and 'to' coordinates + var color = route.type === "train" ? "blue" : "red"; // Green for trains, red for flights + L.polyline([route.from, route.to], {color: color}).addTo(map); + } + }); + + return map; +} diff --git a/templates/trip_list.html b/templates/trip_list.html index d0f5624..d856cae 100644 --- a/templates/trip_list.html +++ b/templates/trip_list.html @@ -106,54 +106,14 @@ integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo=" crossorigin=""> + + + {% endblock %} diff --git a/templates/trip_page.html b/templates/trip_page.html index 2924746..a4157a6 100644 --- a/templates/trip_page.html +++ b/templates/trip_page.html @@ -42,7 +42,7 @@ } #map { - height: 100vh; + height: 80vh; } @@ -91,54 +91,14 @@ integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo=" crossorigin=""> + + + {% endif %}