WME Coordinate Marker Slovenia - traffic reports

rdnnknl
2025-09-11T15:28:57Z

Laat coördinaten DARS info zien op WME

// ==UserScript==
// @name WME Coördinaten Marker
// @namespace rdnnknl
// @version 0.4
// @description Laat coördinaten DARS info zien op WME
// @author Ronald (rdnnk)
// @match https://www.waze.com/*editor*
// @grant GM_xmlhttpRequest
// @connect waze.cc

// ==/UserScript==

(function () {
'use strict';

let customMarkerLayer = null;
let selector = null;

function setupLayer() {
if (customMarkerLayer) return;

// We gebruiken overal W i.p.v. Waze
customMarkerLayer = new OpenLayers.Layer.Vector("RdN Marker Layer", {
displayInLayerSwitcher: true,
uniqueId: "rdn_marker_layer"
});

W.map.addLayer(customMarkerLayer);

selector = new OpenLayers.Control.SelectFeature(customMarkerLayer, {
clickout: true,
onSelect: function (feature) {
const naam = feature.attributes.title || "Onbekend";
const link = feature.attributes.link || null;
const proc = feature.attributes.proc || null;
const prox = feature.attributes.prox || null;
const endd = feature.attributes.endd || null;

console.log("RdN Marker geselecteerd via W object:", naam);

const sidebar = document.getElementById('sidebar');
if (sidebar) {
let rdnInfo = document.getElementById('rdn-info-box');
if (!rdnInfo) {
rdnInfo = document.createElement('div');
rdnInfo.id = 'rdn-info-box';
rdnInfo.style.cssText = "padding: 15px; background: #fff; border: 2px solid #3d7abb; border-radius: 8px; margin: 10px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); font-family: sans-serif;";
sidebar.insertBefore(rdnInfo, sidebar.firstChild);
}

rdnInfo.innerHTML = `

Report:
${naam}

prox :${prox}

closed from:${proc}

end date :${endd}

${(link && link !== "null" && link !== "") ? `

🔗 Open Website
` : ``
}
`;
}
// Direct de-selecteren zodat je weer op een andere marker kunt klikken
this.unselect(feature);
}
});

W.map.addControl(selector);
selector.activate();

// Forceer de laag naar de bovenste positie in de stapel
W.map.setLayerIndex(customMarkerLayer, W.map.layers.length);
// 1. Dwing Chrome om de laag klikbaar te maken via CSS
if (customMarkerLayer.div) {
customMarkerLayer.div.style.pointerEvents = "auto";
}

// 2. Een extreem hoge Z-index voor Chrome
customMarkerLayer.setZIndex(100000);
// -----------------------------------------------

//2. De klik-events registreren (voor beide browsers)
["click", "mousedown"].forEach(function(eventType) {
W.map.events.register(eventType, W.map, function(e) {
if (!customMarkerLayer.visibility) return;

var feature = customMarkerLayer.getFeatureFromEvent(e);
if (feature) {
console.log("RDN: Klik gevangen via " + eventType + " op:", feature.attributes.title);
updateRDNSidebar(feature.attributes);
OpenLayers.Event.stop(e);
return false;
}
});
});

// 3. De "Chrome Fix" & Force Focus
// We zetten de laag op de hoogste plek en dwingen de focus af
W.map.setLayerIndex(customMarkerLayer, W.map.layers.length);

setTimeout(function() {
if (customMarkerLayer.div) {
// Maak de laag fysiek klikbaar voor Chrome
customMarkerLayer.div.style.pointerEvents = "auto";

// "Schud" de laag wakker zodat de browser de klik-rechten vernieuwt
customMarkerLayer.setVisibility(false);
customMarkerLayer.setVisibility(true);

console.log("RDN: Force focus toegepast op de laag.");
}
}, 1000); // Wacht 1 seconde zodat Waze klaar is met opstarten
}

function addMarker(lat, lon, naam, proc, prox, endd, link = null) {
// Cruciaal: Gebruik W.map voor de projectie
const point = new OpenLayers.Geometry.Point(lon, lat).transform(
new OpenLayers.Projection("EPSG:4326"),
W.map.getProjectionObject()
);

const iconUrl = (prox === 'FRGT')
? 'https://maps.gstatic.com/mapfiles/ms2/micons/blue-dot.png'
: (proc === "0000-00-00 00:00:00" || !proc)
? 'https://maps.gstatic.com/mapfiles/ms2/micons/yellow-dot.png'
: 'https://maps.gstatic.com/mapfiles/ms2/micons/green-dot.png';

const feature = new OpenLayers.Feature.Vector(
point,
{ title: naam, link: link, endd:endd, proc:proc, prox:prox },
{
externalGraphic: iconUrl,
graphicHeight: 32,
graphicWidth: 32,
graphicXOffset: -16,
graphicYOffset: -32,
cursor: "pointer",
graphicTitle: naam
}
);

customMarkerLayer.addFeatures([feature]);
}

function clearMarkers() {
if (customMarkerLayer) {
// Unselect alles voor we verwijderen om popup-weeskinderen te voorkomen
if (selector) {
customMarkerLayer.features.forEach(f => selector.unselect(f));
}
customMarkerLayer.removeAllFeatures();
}
}

function loadDataAndAddMarkers() {
if (!W.map || !W.map.getExtent()) return;

const projection4326 = new OpenLayers.Projection("EPSG:4326");
const bounds = W.map.getExtent().clone().transform(
W.map.getProjectionObject(),
projection4326
);

const url = `https://waze.cc/scrpt/slo_promet.php?minLat=${bounds.bottom}&maxLat=${bounds.top}&minLon=${bounds.left}&maxLon=${bounds.right}`;
GM_xmlhttpRequest({
method: "GET",
url: url,
onload: function (response) {
try {
const data = JSON.parse(response.responseText);
clearMarkers();
data.forEach(item => {
//alert(item.naam)
addMarker(
parseFloat(item.lat),
parseFloat(item.lon),
item.naam,
item.PR_PROC,
item.prox,
item.endd,
item.link
);
});
} catch (e) {
console.error("RdN Error:", e);
}
}
});
if (selector) {
selector.deactivate();
selector.activate();
}
}

function init(retries = 0) {
// 1. Basis check of de objecten bestaan
const isReady = typeof W !== "undefined" &&
W.map &&
W.model &&
W.loginManager &&
W.loginManager.user &&
typeof OpenLayers !== "undefined";

if (!isReady) {
if (retries < 30) { // Maximaal 30 seconden proberen
console.log("WME nog niet klaar, even wachten... (" + retries + ")");
setTimeout(() => init(retries + 1), 1000);
}
return;
}

// 2. Extra check: is de kaart-container daadwerkelijk in de DOM?
if (!document.getElementById('WazeMap')) {
setTimeout(() => init(retries + 1), 1000);
return;
}

console.log("WME volledig geladen. Start setup...");

// Nu pas gaan we echt aan de slag
startScript();
}

function startScript() {
setupLayer();
loadDataAndAddMarkers();

// Gebruik W.map.events pas nadat we zeker weten dat W.map bestaat
W.map.events.register("moveend", W.map, loadDataAndAddMarkers);
W.map.events.register("zoomend", W.map, loadDataAndAddMarkers);
}

// Start het proces
init();
})();