OpenStreetMap, eine Alternative zu Google Maps

Mit einem simpelen Beispiel möche ich hier demonstrieren dass mit wenigen Zeilen Code mit OpenStreetMap bereits eine Kartendarstellung und in Verbindung mit Leaflet eine Positionsmarke möglich ist.

HTML:
osm.htm HTML (498 Bytes) 21.05.2023 06:58
<!DOCTYPE html >
<html lang="de">
  <head>
    <meta charset="utf-8" />
    <title>OpenStreetMap</title>
    <meta http-equiv="Content-Security-Policy" content="default-src 'self' data: https://nominatim.openstreetmap.org/; img-src 'self' data: https://tile.openstreetmap.org/" />
  </head>
  <body>
    <div class="map" data-type="osm" data-lat="54.499864" data-lon="9.532111" data-title="Udo Schmal" data-info="Ellerndiek 26
24837 Schleswig, Deutschland"
>
</div> <script src="osm.js"></script> </body> </html>
JavaScript:
osm.js JavaScript (1,78 kByte) 21.05.2023 06:55
// coding: utf-8
/*! Created by: Udo Schmal | https://www.gocher.me/ */
(function () {
  'use strict';

  function OpenStreetMap (item) {
    item.style.width = '100%';
    item.style.height = '300px';
    let lat = item.dataset.lat;
    let lon = item.dataset.lon;
    let zoom = item.dataset.zoom || 18;
    // initialize map
    let map = L.map(item, {scrollWheelZoom: false}).setView([lat, lon], zoom);
    if (map) {
      // set map tiles source
      L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
        attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/" target="_blank">OpenStreetMap</a> contributors',
        maxZoom: 21,
      }).addTo(map);
      let title = item.dataset.title;
      let info = item.dataset.info;
      // add marker to the map
      let marker = L.marker([lat, lon]).addTo(map);
      // add popup to the marker
      marker.bindPopup("<strong>" + title + "</strong><br />" + info);
    }
  }

  var maps = document.querySelectorAll('.map');
  if (maps.length > 0) {
    function init() {
      for (let i=0; i < maps.length; i++) {
        new OpenStreetMap(maps[i]);
      }
    }
    if (!document.getElementById('leaflet.css')) {
      let style = document.createElement('link');
      style.id = 'leaflet.css';
      style.type = 'text/css';
      style.rel = 'stylesheet';
      style.href = '/code/OpenStreetMap/leaflet.css';
      document.head.appendChild(style);
    }
    if (!document.getElementById('leaflet.js')) {
      let script = document.createElement('script');
      script.id = 'leaflet.js';
      script.src = '/code/OpenStreetMap/leaflet.min.js';
      script.addEventListener('load', init);
      document.body.appendChild(script);
    } else {
      init();
    }
  }

})();

Kontakt

Udo Schmal
Udo Schmal

Udo Schmal
Softwareentwickler
Ellerndiek 26
24837 Schleswig
Schleswig-Holstein
Germany




+49 4621 9785538
+49 1575 0663676
+49 4621 9785539
SMS
WhatsApp

Google Maps Profile
Instagram Profile
vCard 2.1, vCard 3.0, vCard 4.0

Service Infos

CMS Info

Product Name:
UDOs Webserver
Version:
0.5.1.123
Description:
All in one Webserver
Copyright:
Udo Schmal
Compilation:
Fri, 14. Jun 2024 06:44:40

Development Info

Compiler:
Free Pascal FPC 3.3.1
compiled for:
OS:Linux, CPU:x86_64

System Info

OS:
Ubuntu 22.04.4 LTS (Jammy Jellyfish)

Hardware Info

Model:
Hewlett-Packard HP Pavilion dm4 Notebook PC
CPU Name:
Intel(R) Core(TM) i5-2430M CPU @ 2.40GHz
CPU Type:
x86_64, 1 physical CPU(s), 2 Core(s), 4 logical CPU(s),  MHz