WME Road Closures

Shows road closures from Waze Live map in WME

目前為 2014-09-19 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name				WME Road Closures
// @description 		Shows road closures from Waze Live map in WME
// @include 			https://www.waze.com/editor/*
// @include 			https://www.waze.com/*/editor/*
// @include 			https://editor-beta.waze.com/*
// @version 			0.1
// @grant				none
// @copyright			2014, pvo11
// @namespace			https://greasyfork.org/scripts/5144-wme-road-closures
// ==/UserScript==

roadClosures_version = '0.1';

var epsg900913 = new OpenLayers.Projection("EPSG:900913");
var epsg4326   = new OpenLayers.Projection("EPSG:4326");
var closuresLayer;


function drawLine(line) {
	var lineFeatures  = [];

	for(var i = 0; i < line.length-1; i++) {
		var lp1 = line[i];
		var lp2 = line[i + 1];
		
		var dif_lon = Math.abs(lp1.x - lp2.x);
		var dif_lat = Math.abs(lp1.y - lp2.y);
		
		if (dif_lon < 0.0000001 && dif_lat < 0.0000001) continue;
		
		var p1 = new OpenLayers.Geometry.Point(lp1.x, lp1.y).transform(epsg4326, epsg900913);
		var p2 = new OpenLayers.Geometry.Point(lp2.x, lp2.y).transform(epsg4326, epsg900913);
		
		
		var points = [];
		points.push(p1);
		points.push(p2);
		var line_part = new OpenLayers.Geometry.LineString(points);
		
		var lineFeature = new OpenLayers.Feature.Vector(line_part, { strokeColor: '#FF0000', labelText: '', strokeWidth: 12 } );
		lineFeatures.push(lineFeature);
	}
	closuresLayer.addFeatures(lineFeatures);
}


function requestClosures()
{
	if (closuresLayer.getVisibility() === true) {
		var extent = Waze.map.getExtent();
		var pLB = new OpenLayers.Geometry.Point(extent.left, extent.bottom).transform(epsg900913, epsg4326);
        var pRT = new OpenLayers.Geometry.Point(extent.right, extent.top).transform(epsg900913, epsg4326);
		var url = 'https://www.waze.com/row-rtserver/web/GeoRSS?os=60&atof=false&format=JSON&ma=200&mj=100&mu=100&jmu=0&types=alerts%2Cusers%2Ctraffic&left=' + pLB.x + '&right=' + pRT.x + '&bottom=' + pLB.y + '&top=' + pRT.y;

		$.ajax({
				dataType: "json",
				url: url,
				success: function(json) {
					if (json.error != undefined) {
				} else {
					if ("undefined" !== typeof(json.jams)) {
						var numjams = json.jams.length;
						for (var i = 0; i < numjams; i++) {
							var jam = json.jams[i];
							if (jam.blockType === "ROAD_CLOSED_EVENT") {
								drawLine(jam.line);
							}
						} 
					}
				}
			}
		});
	}
}


function roadClosures_init()
{
	var style = new OpenLayers.Style({
			strokeDashstyle: 'solid',
			strokeColor : "${strokeColor}",
			strokeOpacity: 1.0,
			strokeWidth: "${strokeWidth}",
			fillColor: '#0040FF',
			fillOpacity: 1.0,
			pointRadius: "${pointRadius}",
			label : "${labelText}",
			fontFamily: "Tahoma, Courier New",
			labelOutlineColor: '#FFFFFF',
			labelOutlineWidth: 0,
			fontColor: "${fontColor}",
			fontOpacity: 1.0,
			fontSize: "10px",
			display: 'block'
		});

	closuresLayer = new unsafeWindow.OpenLayers.Layer.Vector("Road Closures", {
			displayInLayerSwitcher: true,
			uniqueName: "__DrawRouteClosures",
			styleMap: new OL.StyleMap(style)
		});
	I18n.translations.en.layers.name["__DrawRouteClosures"] = "Road Closures";
	unsafeWindow.Waze.map.addLayer(closuresLayer);
	closuresLayer.setVisibility(true);

	Waze.map.events.register("zoomend", null, requestClosures);
	Waze.map.events.register("moveend", null, requestClosures);
	Waze.map.events.register("changelayer", null, requestClosures);
	requestClosures();

}


function roadClosures_bootstrap()
{
	var bGreasemonkeyServiceDefined 	= false;

	try
	{
		bGreasemonkeyServiceDefined = ("object" === typeof Components.interfaces.gmIGreasemonkeyService);
	}
	catch (err)
	{ /* Ignore */ }

	if ( "undefined" === typeof unsafeWindow  ||  ! bGreasemonkeyServiceDefined)
	{
		unsafeWindow	= ( function ()
		{
			var dummyElem	= document.createElement('p');
			dummyElem.setAttribute ('onclick', 'return window;');
			return dummyElem.onclick ();
		} ) ();
	}
	
	setTimeout(roadClosures_init, 3000);
}


roadClosures_bootstrap();