[openstreetmap/openstreetmap-website] Trace display on map (#387)

mmd notifications at github.com
Wed Jul 31 21:32:52 UTC 2024


For discussion:

I had a quick look, if we can offer this feature with very little effort. It seems https://github.com/mpetazzoni/leaflet-gpx could good be a good starting point. It offers gpx parsing/rendering out of the box.

I haven't tested if this works with zip files. If in doubt we could show some error message, or simply a start point like we do today. Private traces are also visible (like on the example screenshots below), as long as the user is logged on.

![image](https://github.com/user-attachments/assets/9beb3c6d-1512-46d4-a411-32ac50a19f74)

Click on "View Map"

GPX trace is shown on map, including start and end point. 

![image](https://github.com/user-attachments/assets/a8a36621-4f1e-4a9c-86bf-447d1b23e614)


Based on a bit of prototyping:

```
diff --git a/app/assets/javascripts/embed.js.erb b/app/assets/javascripts/embed.js.erb
index 9a0ec07d3..159a67fc7 100644
--- a/app/assets/javascripts/embed.js.erb
+++ b/app/assets/javascripts/embed.js.erb
@@ -2,6 +2,7 @@
 //= depend_on settings.local.yml
 //= require leaflet/dist/leaflet-src
 //= require leaflet.osm
+//= require leaflet-gpx
 //= require i18n
 //= require i18n/embed
 
@@ -51,6 +52,19 @@ window.onload = function () {
     new L.OSM.HOT().addTo(map);
   }
 
+  if (args.trace && /^\d+$/.test(args.trace)) {
+    var url = `/trace/${args.trace}/data.xml`;
+    new L.GPX(url, {
+        async: true,
+        markers: {
+         startIcon: <%= asset_path('leaflet-gpx/pin-icon-start.png').to_json %>,
+         endIcon: <%= asset_path('leaflet-gpx/pin-icon-end.png').to_json %>,
+        }
+      }).on('loaded', function(e) {
+      map.fitBounds(e.target.getBounds());
+    }).addTo(map);
+  }
+
   if (args.marker) {
     L.marker(args.marker.split(','), {icon: L.icon({
       iconUrl: <%= asset_path('leaflet/dist/images/marker-icon.png').to_json %>,
diff --git a/app/views/traces/_trace.html.erb b/app/views/traces/_trace.html.erb
index f60eea7f1..0fa021161 100644
--- a/app/views/traces/_trace.html.erb
+++ b/app/views/traces/_trace.html.erb
@@ -49,7 +49,7 @@
       <nav class="secondary-actions">
         <ul>
           <li>
-            <%= link_to t(".view_map"), root_path(:mlat => trace.latitude, :mlon => trace.longitude, :anchor => "map=14/#
{trace.latitude}/#{trace.longitude}") %>
+            <%= link_to t(".view_map"), export_embed_path(:trace => trace.id) %>
           </li>
           <li>
             <%= link_to t(".edit_map"), edit_path(:gpx => trace.id) %>
diff --git a/package.json b/package.json
index 7d19be0d2..27d8acf67 100644
--- a/package.json
+++ b/package.json
@@ -6,6 +6,7 @@
     "js-cookie": "^3.0.0",
     "leaflet": "^1.8.0",
     "leaflet.locatecontrol": "^0.81.0",
+    "leaflet-gpx": "^2.0.0",
     "osm-community-index": "^5.2.0",
     "qs": "^6.9.4"
   },

```   

-- 
Reply to this email directly or view it on GitHub:
https://github.com/openstreetmap/openstreetmap-website/issues/387#issuecomment-2261498632
You are receiving this because you are subscribed to this thread.

Message ID: <openstreetmap/openstreetmap-website/issues/387/2261498632 at github.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openstreetmap.org/pipermail/rails-dev/attachments/20240731/0f855af1/attachment.htm>


More information about the rails-dev mailing list