WME LiveMap Alerts Overlay

Overlay alerts from the Waze LiveMap.

当前为 2014-09-18 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name                WME LiveMap Alerts Overlay
// @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
// @grant               none
// @namespace           
// ==/UserScript==


//------------------------------------------------------------------------------------------------
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);
    
  /*var geometry = imageFeature.geometry;
  var coordinate = new OpenLayers.LonLat(geometry.x, geometry.y);
  var pixel = Waze.map.getPixelFromLonLat(coordinate);
    console.log('WME LMAO: feature xy position ' + pixel);*/
}

//--------------------------------------------------------------------------------------------------------
/*
function customListener(){

    var test = document.getElementsByClassName("olControlMousePosition").innerHTML;
    console.log('WME LMAO: Custom mouse move listener, mouse position ' + test);
    
}
*/

//--------------------------------------------------------------------------------------------------------
function initializeLiveMapAlerts()
{    
    console.log("WME LMAO: Initializing");
    
    LiveMapAlerts_Layer = new OL.Layer.Vector("LiveMap Alerts",{
            rendererOptions: { zIndexing: true }, 
     		uniqueName: 'livemap_alerts'
        }          
    ); 
    
    function showAlertPopup(f){
        var attributes = f.attributes;
        //console.log('WME LMAO: Show details for: ' + attributes.city);
        
        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';
    };
    
    function showAlertDetails(e){
        
        var feature = e.feature;
        console.log("WME LMAO: Show details for: " + feature.attributes.type);
        
        var alertType = ((feature.attributes.type == "WEATHERHAZARD") ? "HAZARD" : feature.attributes.type);
        var alertSubType = ((feature.attributes.subtype == null) ? "" : feature.attributes.subtype);
        var alertDescription = ((feature.attributes.description == null) ? "" : feature.attributes.description);
        var alertStreet = ((feature.attributes.street == null) ? "" : feature.attributes.street);
        var alertCity = ((feature.attributes.city == null) ? "" : feature.attributes.city);
        var alertNear = ((feature.attributes.near == null) ? "" : feature.attributes.near);
        var alertReportBy = ((feature.attributes.reportby == null) ? "" : feature.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 hideAlertDetails(e){
        divLMAO.style.visibility = 'hidden';
    };
    
    var highlightCtrl = new OpenLayers.Control.SelectFeature(LiveMapAlerts_Layer, {
        hover: true,
        highlightOnly: true,
        renderIntent: "temporary",
        eventListeners: {
        	beforefeaturehighlighted: hideAlertDetails,
            featurehighlighted: showAlertDetails,
            featureunhighlighted: hideAlertDetails
        }
    });
    
    var selectCtrl = new OpenLayers.Control.SelectFeature(LiveMapAlerts_Layer,
    	{clickout: true}
    );

    LiveMapAlerts_Layer.setZIndex(9999);
    Waze.map.addLayer(LiveMapAlerts_Layer);
    Waze.map.addControl(new OL.Control.DrawFeature(LiveMapAlerts_Layer, OL.Handler.Path));
    
    Waze.map.addControl(highlightCtrl);
    Waze.map.addControl(selectCtrl);
    
    //Waze.selectionManager.selectControl.deactivate();
    //highlightCtrl.activate();
    
    //selectCtrl.activate();
    //Waze.selectionManager.selectControl.activate();

    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.addControl(
                new OpenLayers.Control.MousePosition({
                    prefix: '<a target="_blank" ' +
                        'href="http://spatialreference.org/ref/epsg/4326/">' +
                        'EPSG:4326</a> coordinates: ',
                    separator: ' | ',
                    numDigits: 2,
                    emptyString: 'Mouse is not over map.'
                })
            );*/
    
    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);
    
    //window.setInterval(customListener, 500);
}

//--------------------------------------------------------------------------------------------------------------
bootstrapLiveMapAlerts();