Altes Köln

Widget:LeafletSMWMap: Unterschied zwischen den Versionen

Aus Altes Köln
Wechseln zu:Navigation, Suche
Keine Bearbeitungszusammenfassung
Keine Bearbeitungszusammenfassung
 
(6 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 1: Zeile 1:
<div id="smwmap" style="width:100%; height:500px;"></div>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" />
 
<link rel="stylesheet"
      href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" />
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>


<script>
<script>
(async function () {
(async function () {
  const el = document.getElementById('smwmap');
  if (!el) {
    console.log('LeafletSMWMap: #smwmap nicht gefunden');
    return;
  }


   // const jsonUrl  = '{{{json}}}';
   const ask = el.dataset.query;
const ask = '[[Kategorie:Schule]] [[Position::+]]|?Position|?Schultyp|limit=50';//nur testweise
  const jsonUrl = '/api.php?action=ask&format=json&query=' + encodeURIComponent(ask);
const jsonUrl = '/api.php?action=ask&format=json&query=' + encodeURIComponent(ask);//nur testweise


  const posProp  = el.dataset.position || 'Position';
  const typeProp = el.dataset.type || 'Schultyp';
  const iconSpec = el.dataset.iconmap || '';
  const defIcon  = el.dataset.defaulticon || 'Marker 11 rot.png';


  console.log('jsonUrl=', jsonUrl);


  const posProp  = '{{{position}}}';
   // Icon-Mapping "Typ=Dateiname;Typ2=Dateiname2"
  const typeProp  = '{{{type}}}';
  const iconSpec  = '{{{iconMap}}}';
  const defIcon  = '{{{defaultIcon}}}';
 
   // Icon-Mapping "Typ=Datei.png;Typ2=Datei2.png"
   const iconMap = {};
   const iconMap = {};
   iconSpec.split(';').forEach(pair => {
   if (iconSpec.trim().length) {
    const [k,v] = pair.split('=');
    iconSpec.split(';').forEach(pair => {
    if (k && v) {
      const parts = pair.split('=');
      iconMap[k.trim()] =
      if (parts.length >= 2) {
        '/wiki/Spezial:Dateipfad/' + encodeURIComponent(v.trim());
        const k = parts[0].trim();
     }
        const v = parts.slice(1).join('=').trim();
   });
        if (k && v) {
   const defaultIcon =
          iconMap[k] = '/wiki/Spezial:Dateipfad/' + encodeURIComponent(v);
    '/wiki/Spezial:Dateipfad/' + encodeURIComponent(defIcon);
        }
 
      }
  const data = await (await fetch(jsonUrl, { credentials:'same-origin' })).json();
     });
   }
   const defaultIcon = '/wiki/Spezial:Dateipfad/' + encodeURIComponent(defIcon);


//temporäres Debugging
  // JSON laden
const resp = await fetch(jsonUrl, { credentials: 'same-origin' });
  const resp = await fetch(jsonUrl, { credentials: 'same-origin' });
const text = await resp.text();
  if (!resp.ok) throw new Error('SMW JSON nicht erreichbar: ' + resp.status + ' ' + resp.statusText);
console.log('JSON-URL:', jsonUrl);
  const data = await resp.json();
console.log('RAW JSON (erste 500 Zeichen):', text.slice(0, 500));


const data = JSON.parse(text);
// debug
console.log('PARSED DATA:', data);
//Ende temporäres Debugging


console.log('SMW JSON keys:', Object.keys(data || {}));
console.log('SMW meta:', data?.query?.meta);
console.log('Results count:', data?.query?.meta?.count, 'Result keys sample:', Object.keys(data?.query?.results || {}).slice(0, 5));


// Ende debug


// Leaflet initialisieren (el ist bereits die Map-DIV)
  const map = L.map(el);
  L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
    maxZoom: 19,
    attribution: '&copy; OpenStreetMap'
  }).addTo(map);


const bounds = [];
const results = data?.query?.results || {};


for (const key in results) {
  const r = results[key];              // r wird hier definiert
  const po = r.printouts || {};        // erst danach benutzen


  // OPTIONAL: Debug ohne r-vorher-Fehler
  // console.log('Printouts keys:', Object.keys(po));


  const posArr = po[posProp] || [];
  if (!Array.isArray(posArr) || !posArr.length) continue;


   const map = L.map('smwmap');
   const pos = posArr[0];
   L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
   if (!pos || typeof pos.lat !== 'number' || typeof pos.lon !== 'number') continue;
    { maxZoom: 19, attribution: '&copy; OpenStreetMap' }
  ).addTo(map);


   const bounds = [];
   const typeArr = po[typeProp] || [];
   const results = data.query?.results || {};
   const typ = (Array.isArray(typeArr) && typeArr.length) ? String(typeArr[0]) : '';


   for (const key in results) {
   const iconUrl = iconMap[typ] || defaultIcon;
    const = results[key];
  const icon = L.icon({ iconUrl, iconSize: [24, 24], iconAnchor: [12, 24] });
    const pos = r.printouts?.[posProp]?.[0];
    if (!pos || typeof pos.lat !== 'number') continue;


     const type = r.printouts?.[typeProp]?.[0] || '';
  L.marker([pos.lat, pos.lon], { icon })
    const iconUrl = iconMap[type] || defaultIcon;
    .addTo(map)
     .bindPopup(`<a href="${r.fullurl}">${r.fulltext}</a><br><b>${typeProp}:</b> ${typ || '-'}`);


    const icon = L.icon({
  bounds.push([pos.lat, pos.lon]);
      iconUrl, iconSize:[24,24], iconAnchor:[12,24]
}
    });
 
    L.marker([pos.lat, pos.lon], { icon })
      .addTo(map)
      .bindPopup(
        `<a href="${r.fullurl}">${r.fulltext}</a><br>${type}`
      );
 
    bounds.push([pos.lat, pos.lon]);
  }


  bounds.length ? map.fitBounds(bounds,{padding:[20,20]})
                : map.setView([50.94,6.96],12);


  if (bounds.length) map.fitBounds(bounds, { padding: [20, 20] });
  else map.setView([50.94, 6.96], 12);
})();
})();
</script>
</script>

Aktuelle Version vom 17. Dezember 2025, 20:32 Uhr

<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" /> <script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>

<script> (async function () {

 const el = document.getElementById('smwmap');
 if (!el) {
   console.log('LeafletSMWMap: #smwmap nicht gefunden');
   return;
 }
 const ask = el.dataset.query;
 const jsonUrl = '/api.php?action=ask&format=json&query=' + encodeURIComponent(ask);
 const posProp  = el.dataset.position || 'Position';
 const typeProp = el.dataset.type || 'Schultyp';
 const iconSpec = el.dataset.iconmap || ;
 const defIcon  = el.dataset.defaulticon || 'Marker 11 rot.png';
 console.log('jsonUrl=', jsonUrl);
 // Icon-Mapping "Typ=Dateiname;Typ2=Dateiname2"
 const iconMap = {};
 if (iconSpec.trim().length) {
   iconSpec.split(';').forEach(pair => {
     const parts = pair.split('=');
     if (parts.length >= 2) {
       const k = parts[0].trim();
       const v = parts.slice(1).join('=').trim();
       if (k && v) {
         iconMap[k] = '/wiki/Spezial:Dateipfad/' + encodeURIComponent(v);
       }
     }
   });
 }
 const defaultIcon = '/wiki/Spezial:Dateipfad/' + encodeURIComponent(defIcon);
 // JSON laden
 const resp = await fetch(jsonUrl, { credentials: 'same-origin' });
 if (!resp.ok) throw new Error('SMW JSON nicht erreichbar: ' + resp.status + ' ' + resp.statusText);
 const data = await resp.json();

// debug

console.log('SMW JSON keys:', Object.keys(data || {})); console.log('SMW meta:', data?.query?.meta); console.log('Results count:', data?.query?.meta?.count, 'Result keys sample:', Object.keys(data?.query?.results || {}).slice(0, 5));

// Ende debug

// Leaflet initialisieren (el ist bereits die Map-DIV)

 const map = L.map(el);
 L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
   maxZoom: 19,
   attribution: '© OpenStreetMap'
 }).addTo(map);
const bounds = [];

const results = data?.query?.results || {};

for (const key in results) {

 const r = results[key];              // r wird hier definiert
 const po = r.printouts || {};         // erst danach benutzen
 // OPTIONAL: Debug ohne r-vorher-Fehler
 // console.log('Printouts keys:', Object.keys(po));
 const posArr = po[posProp] || [];
 if (!Array.isArray(posArr) || !posArr.length) continue;
 const pos = posArr[0];
 if (!pos || typeof pos.lat !== 'number' || typeof pos.lon !== 'number') continue;
 const typeArr = po[typeProp] || [];
 const typ = (Array.isArray(typeArr) && typeArr.length) ? String(typeArr[0]) : ;
 const iconUrl = iconMap[typ] || defaultIcon;
 const icon = L.icon({ iconUrl, iconSize: [24, 24], iconAnchor: [12, 24] });
 L.marker([pos.lat, pos.lon], { icon })
   .addTo(map)
   .bindPopup(`<a href="${r.fullurl}">${r.fulltext}</a>
${typeProp}: ${typ || '-'}`);
 bounds.push([pos.lat, pos.lon]);

}


 if (bounds.length) map.fitBounds(bounds, { padding: [20, 20] });
 else map.setView([50.94, 6.96], 12);

})(); </script>

Cookies helfen uns bei der Bereitstellung von Altes Köln. Durch die Nutzung von Altes Köln erklärst du dich damit einverstanden, dass wir Cookies speichern.