- // ==UserScript==
- // @name WME LiveMap Alerts Overlay - beta
- // @author davielde
- // @description Overlay alerts from the Waze LiveMap.
- // @include https://www.waze.com/editor/*
- // @include https://www.waze.com/*/editor/*
- // @include https://editor-beta.waze.com/*
- // @version 0.1.5
- // @grant none
- // @namespace https://gf.qytechs.cn/users/5252
- // ==/UserScript==
-
- // Many thanks to Timbones and Twister-UK, whose code (WMECH and URO+) provided a large foundation for this work
-
- //------------------------------------------------------------------------------------------------
- function bootstrapLiveMapAlerts()
- {
- var bGreasemonkeyServiceDefined = false;
-
- try {
- bGreasemonkeyServiceDefined = (typeof Components.interfaces.gmIGreasemonkeyService === "object");
- }
- catch (err) { /* Ignore */ }
-
- if (typeof unsafeWindow === "undefined" || ! bGreasemonkeyServiceDefined) {
- unsafeWindow = ( function () {
- var dummyElem = document.createElement('p');
- dummyElem.setAttribute('onclick', 'return window;');
- return dummyElem.onclick();
- }) ();
- }
-
- setTimeout(initializeLiveMapAlerts, 999);
-
- }
-
- //--------------------------------------------------------------------------------------------------------
- function getBounds()
- {
- var alertBounds = Waze.map.getExtent();
-
- alertBounds.transform(new OpenLayers.Projection("EPSG:900913"),new OpenLayers.Projection("EPSG:4326"));
- console.log('WME LMAO: Current bounds = Left ' + alertBounds.left + ', Right ' + alertBounds.right + ', Bottom ' + alertBounds.bottom + ', Top ' + alertBounds.top);//verify transform
-
- return alertBounds;
- }
-
- //--------------------------------------------------------------------------------------------------------
- function getLiveMapAlerts(){
-
- LiveMapAlerts_Layer.destroyFeatures();
-
- var alertBounds = getBounds();
- var url = "/rtserver/web/GeoRSS";
- var data = {
- format: "JSON",
- types: "alerts",
- left: alertBounds.left,
- right: alertBounds.right,
- bottom: alertBounds.top,
- top: alertBounds.bottom
- };
-
- $.ajax({
- dataType: "json",
- url: url,
- data: data,
- success: function(json) {
-
- var alertData = json.alerts;
- try {
- for(var i=0; i<alertData.length; i++) {
- var lat = alertData[i].location.y;
- var long = alertData[i].location.x;
- var image = alertData[i].type;
- var title = alertData[i].reportDescription;
-
- console.log("WME LMAO: " + alertData[i].type, alertData[i].location.x, alertData[i].location.y);
- addImage(lat,long,image,alertData[i]);
- }
- }
- catch(e) {
- console.log('WME LMAO: No alerts in view');
- }
- }
- });
- }
-
- //--------------------------------------------------------------------------------------------------------
- function addImage(lat, long, type, detail) {
-
- var coords = OpenLayers.Layer.SphericalMercator.forwardMercator(long, lat);
- var point = new OL.Geometry.Point(coords.lon,coords.lat);
- var alertPx = Waze.map.getPixelFromLonLat(new OpenLayers.LonLat(coords.lon,coords.lat));
- var imgRoot = '/assets';
-
- switch(type){
- case 'ROAD_CLOSED':
- var icon = '/livemap/alerts/road_closed-243aa9787857ee55f762c52ceb13813b.png';
- break;
- case 'ACCIDENT':
- var icon = '/livemap/alerts/accident-c5f76aaa426f2731d2e10c6ac99fe918.png';
- break;
- case 'JAM':
- var icon = '/livemap/alerts/jam-8269807a1e2a79c944a9577483ac8df7.png';
- break;
- case 'POLICEMAN': //Chrome
- var icon = '/livemap/alerts/police-503317f465b6295c7bf084ff773848e3.png';
- break;
- case 'POLICE': //Firefox
- var icon = '/livemap/alerts/police-503317f465b6295c7bf084ff773848e3.png';
- break;
- case 'HAZARD': //Firefox
- var icon = '/livemap/alerts/hazard-ed111f132551125f297b76ef4cca9101.png';
- break;
- case 'WEATHERHAZARD': //Chrome returns this for *all* hazards...
- var icon = '/livemap/alerts/hazard-ed111f132551125f297b76ef4cca9101.png';
- break;
- default:
- var icon = '/livemap/alerts/hazard-ed111f132551125f297b76ef4cca9101.png';
- };
- var attributes = {
- type: detail.type,
- subtype: detail.subtype,
- description: detail.reportDescription,
- street: detail.street,
- city: detail.city,
- near: detail.nearBy,
- reportby: detail.reportBy,
- pixel: alertPx
- };
-
- var style = {
- externalGraphic: imgRoot + icon,
- graphicWidth: 30,
- graphicHeight: 32,
- graphicZIndex: 9999,
- title: 'LiveMap',
- cursor: 'help'
- };
-
- var imageFeature = new OL.Feature.Vector(point, attributes, style);
-
- LiveMapAlerts_Layer.addFeatures([imageFeature]);
- //console.log('WME LMAO: Added alert at ' + lat + ',' + long);
-
- }
-
- //--------------------------------------------------------------------------------------------------------
- function initializeLiveMapAlerts()
- {
- console.log("WME LMAO: Initializing");
-
- var lmaoVisibility = true;
-
- // restore saved settings
- if (localStorage.WMELiveMapAlerts) {
- console.log("WME LMAO: loading options");
- var options = JSON.parse(localStorage.getItem("WMELiveMapAlerts"));
-
- lmaoVisibility = options[0];
- }
-
- // overload the WME exit function
- saveLMAOOptions = function() {
- if (localStorage) {
- console.log("WME FC Highlights: saving options");
- var options = [];
-
- lmaoVisibility = LiveMapAlerts_Layer.visibility;
- options[0] = lmaoVisibility;
-
- localStorage.setItem("WMELiveMapAlerts", JSON.stringify(options));
- }
- }
- window.addEventListener("beforeunload", saveLMAOOptions, false);
-
-
- LiveMapAlerts_Layer = new OL.Layer.Vector("LiveMap Alerts",{
- rendererOptions: { zIndexing: true },
- uniqueName: 'livemap_alerts'
- }
- );
-
- function showAlertPopup(f){
- var attributes = f.attributes;
-
- var alertType = ((attributes.type == "WEATHERHAZARD") ? "HAZARD" : attributes.type);
- var alertSubType = ((attributes.subtype == null) ? "" : attributes.subtype);
- var alertDescription = ((attributes.description == null) ? "" : attributes.description);
- var alertStreet = ((attributes.street == null) ? "" : attributes.street);
- var alertCity = ((attributes.city == null) ? "" : attributes.city);
- var alertNear = ((attributes.near == null) ? "" : attributes.near);
- var alertReportBy = ((attributes.reportby == null) ? "" : attributes.reportby);
-
- var reportDetail = "<b>LiveMap Alert Details</b>"
- + "<br>TYPE: " + alertType
- + "<br>SUBTYPE: " + alertSubType
- + "<br>DESCRIPTION: " + alertDescription
- + "<br>STREET: " + alertStreet
- + "<br>CITY: " + alertCity
- + "<br>NEAR: " + alertNear
- + "<br>REPORT BY: " + alertReportBy
- ;
- document.getElementById("divLMAO").innerHTML = reportDetail;
- divLMAO.style.visibility = 'visible';
- };
-
- function hideAlertPopup(){
- divLMAO.style.visibility = 'hidden';
- };
-
-
- LiveMapAlerts_Layer.setZIndex(9999);
- Waze.map.addLayer(LiveMapAlerts_Layer);
- Waze.map.addControl(new OL.Control.DrawFeature(LiveMapAlerts_Layer, OL.Handler.Path));
-
-
- divLMAO = document.createElement('div');
- divLMAO.id = "divLMAO";
- divLMAO.style.position = 'absolute';
- divLMAO.style.visibility = 'hidden';
- divLMAO.style.top = '175px';
- divLMAO.style.left = '375px';
- divLMAO.style.zIndex = 1000;
- divLMAO.style.backgroundColor = 'aliceblue';
- divLMAO.style.borderWidth = '3px';
- divLMAO.style.borderStyle = 'ridge';
- divLMAO.style.borderRadius = '10px';
- divLMAO.style.boxShadow = '5px 5px 10px Silver';
- divLMAO.style.padding = '4px';
- document.body.appendChild(divLMAO);
-
-
- //clear existing LMAO features
- LiveMapAlerts_Layer.destroyFeatures();
-
- var lmaoLayer = null;
- for(i=0; i<Waze.map.layers.length; i++)
- {
- if(Waze.map.layers[i].uniqueName == 'livemap_alerts') lmaoLayer = i;
- }
- //console.log('WME LMAO: layer number = ' + lmaoLayer);
-
-
- Waze.map.events.register("mousemove", Waze.map, function(e) {
- hideAlertPopup();
- var position = this.events.getMousePosition(e);
- console.log('WME LMAO: coords xy = ' + position.x + ' ' + position.y);
-
- if(Waze.map.layers[lmaoLayer].features.length > 0){
-
- var alertCount = Waze.map.layers[lmaoLayer].features.length;
- console.log('WME LMAO: Current LiveMap alert count = ' + alertCount);
-
- var alertFeatures = Waze.map.layers[lmaoLayer];
- for(j=0; j<Waze.map.layers[lmaoLayer].features.length; j++){
-
- var alertX = alertFeatures.features[j].attributes.pixel.x;
- var alertY = alertFeatures.features[j].attributes.pixel.y;
- if(position.x > alertX - 10 && position.x < alertX + 10 && position.y > alertY - 10 && position.y < alertY + 10){
- console.log('WME LMAO: hover over alert');
- showAlertPopup(alertFeatures.features[j]);
- }
- }
- }
- });
-
- window.setTimeout(getLiveMapAlerts(), 500);
- //window.setInterval(getLiveMapAlerts(), 60000); //refresh every minute
-
- //refresh if user moves map
- Waze.map.events.register("moveend", Waze.map, getLiveMapAlerts);
-
- window.setTimeout(getLiveMapAlerts(), 500);
-
- }
-
- //--------------------------------------------------------------------------------------------------------------
- bootstrapLiveMapAlerts();