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 (497 bytes) 17.01.2022 23:54
<!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="51.528710" data-lon="6.289250" data-title="Udo Schmal" data-info="Olvengraben 41
47608 Geldern, Deutschland"
>
</div> <script src="osm.js"></script> </body> </html>
JavaScript:
osm.js JavaScript (1,77 kByte) 18.01.2022 19:48
// 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/">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();
    }
  }

})();

Author: , published: , last modified:

Kontakt

Udo Schmal

Udo Schmal
Softwareentwickler
Olvengraben 41
47608 Geldern
Nordrhein-Westfalen
Germany




+49 2831 9776557
+49 1575 0663676
+49 2831 1328709
SMS
WhatsApp

Google Maps Profile
Instagram Profile
vCard 3.0

Service Infos

CMS Info

Product Name:
UDOs Webserver
Version:
0.5.0.162
Description:
All in one Webserver
Copyright:
Udo Schmal
Compilation:
Thu, 14. Apr 2022 20:04:40

Development Info

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

System Info

OS:
Ubuntu 20.04.4 LTS (Focal Fossa)

Hardware Info

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