Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Support

Map-marker-leaflet and GPX tracks

Solved by p View solution

Started by p 1 year ago · 2 replies · 190 views
1 year ago

Hi,

I recently added the map-marker-leaflet plugin and embedding it and adding markers worked out of the box. Now I'd like to add a GPX track. Therefore I tried to go the way described in https://github.com/mpetazzoni/leaflet-gpx, but without success. I used the shortcode-assets plugin to add the js file to the header and this also seemed to work. But now I'm struggling with adding the GPX track itself.

Currently my page looks like this:

JS
---
title: Maps
cache_enable: false
---
# San Fransisco Transport
[assets=js]
https://cdnjs.cloudflare.com/ajax/libs/leaflet-gpx/2.1.2/gpx.min.js
[/assets]

[map-leaflet lat=37.7749 lng=-122.4194 zoom=13 mapname=transd variant=transport-dark]
[a-markers markerColor="lightblue" iconColor="white"]
[{"lat": 37.7749, "lng": -122.4194, "icon": "home", "title": "Home Position"}]
[/a-markers]
[a-markers icon=""]
[
{"lat": 37.775, "lng": -122.48 , "text": 1, "draggable": true},
{"lat": 37.77, "lng": -122.414 , "text": 2, "markerColor": "pink"},
{"lat": 37.765, "lng": -122.409, "text": 3, "spin": true},
{"lat": 37.76, "lng": -122.3995, "text": 4, "spin": false},
{"lat": 37.755, "lng": -122.499, "icon": "coffee", "markerColor": "lightred", "title": "Lovely bistro"}
]
[/a-markers]
[/map-leaflet]

[assets=inlineJs loading="async defer" group="bottom"]
// URL to your GPX file or the GPX itself as a XML string.
const url = 'https://mpetazzoni.github.io/leaflet-gpx/demo.gpx';
const options = {
async: true,
polyline_options: { color: 'red' },
};

const gpx = new L.GPX(url, options).on('loaded', (e) => {
map.fitBounds(e.target.getBounds());
}).addTo(map);
[/assets]

Was anyone already successful in combining this? Or could give me some hints anyhow?

last edited 04/17/25 by p
1 year ago Solution

I was able to get it working with the other assets plugin. In the end it looks similar to this.

JS
---
title: Maps
cache_enable: false
---
# San Fransisco Transport
{assets:css order:5}
http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css
{/assets}

{assets:js order:5}
http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js
https://cdnjs.cloudflare.com/ajax/libs/leaflet-gpx/2.1.2/gpx.min.js
{/assets}

{assets:inline_css}
div#map {width: 100%; height: 600px;}
{/assets}

{assets:inline_js}
$(document).ready(function() {
    var map = L.map('map').setView([37.7749, -122.4194], 13);

    L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
        attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors', maxZoom: 19
    }).addTo(map);

    L.marker([37.775, -122.48], {
        icon: L.ExtraMarkers.icon({markerColor: 'red', iconColor: 'white', icon: 'home'}), title: "1"
    }).addTo(map);

    L.marker([37.77, -122.414], {
        icon: L.ExtraMarkers.icon({markerColor: 'red', iconColor: 'white', icon: 'home'}), title: "2"
    }).addTo(map);

    // URL to your GPX file or the GPX itself as a XML string.
    const url = 'https://mpetazzoni.github.io/leaflet-gpx/demo.gpx';
    const options = {
        async: true, polyline_options: { color: 'red' },
    };
    const gpx = new L.GPX(url, options).addTo(map);

});
{/assets}

<div id="map"></div>

👍 1
1 year ago

Thanks for sharing your solution here.

@pikim:
I was able to get it working with the other assets plugin.

Which other assets plugin, @pikim?

Suggested topics

Topic Participants Replies Views Activity
Support · by Thomas, 1 week ago
2 51 8 hours ago
Support · by Anna, 2 days ago
2 58 10 hours ago
Support · by Justin Young, 11 hours ago
1 30 11 hours ago
Support · by Duc , 1 week ago
2 63 5 days ago
Support · by Colin Hume, 1 week ago
2 55 5 days ago