Repository with sources and generator of https://larlet.fr/david/ https://larlet.fr/david/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2024-03-26 - GPX Viewer.html 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <gpx-viewer data-height="500px" data-width="800px">
  2. <p>
  3. Vous devriez voir s’afficher une carte de mon « Grand Tour de la forêt de Ouareau »,
  4. qui contient <a data-gpx href="/static/david/2024/grand_tour_de_la_foret_de_ouareau.gpx">cette trace GPX</a>,
  5. centrée sur <span data-latitude>46.2117</span>, <span data-longitude>-73.9335</span>
  6. avec un zoom de <span data-zoom>12</span>.
  7. Il est probable que cela ne s’exécute pas dans un agrégateur par exemple.
  8. </p>
  9. </gpx-viewer>
  10. <script type="module">
  11. import * as L from '/static/david/2024/leaflet.1.9.4/leaflet.1.9.4.js'
  12. window.L = L // Hack for leaflet-gpx, youpi.
  13. </script>
  14. <script type="module" nonce="oembed-web-component">
  15. class GPXViewer extends HTMLElement {
  16. static tagName = 'gpx-viewer'
  17. static register(tagName, registry) {
  18. if(!registry && ('customElements' in globalThis)) {
  19. registry = globalThis.customElements
  20. }
  21. registry?.define(tagName || this.tagName, this)
  22. }
  23. #attachCSS(path) {
  24. const linkElement = document.createElement('link')
  25. linkElement.setAttribute('rel', 'stylesheet')
  26. linkElement.setAttribute('href', path)
  27. this.shadowRoot.appendChild(linkElement)
  28. }
  29. #createMapContainer() {
  30. const mapContainer = document.createElement('div')
  31. mapContainer.style.height = this.dataset.height
  32. mapContainer.style.width = this.dataset.width
  33. this.shadowRoot.appendChild(mapContainer)
  34. return mapContainer
  35. }
  36. #createMap(mapContainer) {
  37. const map = L.map(mapContainer).setView(
  38. [
  39. this.querySelector('[data-latitude]').textContent,
  40. this.querySelector('[data-longitude]').textContent,
  41. ],
  42. this.querySelector('[data-zoom]').textContent
  43. )
  44. L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
  45. attribution:
  46. '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
  47. }).addTo(map)
  48. return map
  49. }
  50. #attachGPX(map) {
  51. const gpxUrl = this.querySelector('[data-gpx]').href
  52. new GPX(gpxUrl, {
  53. async: true,
  54. marker_options: {
  55. startIconUrl: '/static/david/2024/leaflet-gpx.1.7.0-custom/pin-icon-start.png',
  56. endIconUrl: '/static/david/2024/leaflet-gpx.1.7.0-custom/pin-icon-end.png',
  57. shadowUrl: '/static/david/2024/leaflet-gpx.1.7.0-custom/pin-shadow.png'
  58. }
  59. }).on('loaded', (e) => {
  60. map.fitBounds(e.target.getBounds())
  61. }).addTo(map)
  62. }
  63. constructor() {
  64. super()
  65. this.attachShadow({ mode: 'open' })
  66. }
  67. async connectedCallback() {
  68. this.#attachCSS('/static/david/2024/leaflet.1.9.4/leaflet.css')
  69. const mapContainer = this.#createMapContainer()
  70. const map = this.#createMap(mapContainer)
  71. this.#attachGPX(map)
  72. }
  73. }
  74. import GPX from '/static/david/2024/leaflet-gpx.1.7.0-custom/gpx.1.7.0-custom.js'
  75. GPXViewer.register()
  76. </script>