WME LiveMap Alerts Overlay - beta

Overlay alerts from the Waze LiveMap.

目前为 2014-09-19 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name WME LiveMap Alerts Overlay - beta
  3. // @author davielde
  4. // @description Overlay alerts from the Waze LiveMap.
  5. // @include https://www.waze.com/editor/*
  6. // @include https://www.waze.com/*/editor/*
  7. // @include https://editor-beta.waze.com/*
  8. // @version 0.1.5
  9. // @grant none
  10. // @namespace https://gf.qytechs.cn/users/5252
  11. // ==/UserScript==
  12.  
  13. // Many thanks to Timbones and Twister-UK, whose code (WMECH and URO+) provided a large foundation for this work
  14.  
  15. //------------------------------------------------------------------------------------------------
  16. function bootstrapLiveMapAlerts()
  17. {
  18. var bGreasemonkeyServiceDefined = false;
  19. try {
  20. bGreasemonkeyServiceDefined = (typeof Components.interfaces.gmIGreasemonkeyService === "object");
  21. }
  22. catch (err) { /* Ignore */ }
  23. if (typeof unsafeWindow === "undefined" || ! bGreasemonkeyServiceDefined) {
  24. unsafeWindow = ( function () {
  25. var dummyElem = document.createElement('p');
  26. dummyElem.setAttribute('onclick', 'return window;');
  27. return dummyElem.onclick();
  28. }) ();
  29. }
  30. setTimeout(initializeLiveMapAlerts, 999);
  31.  
  32. }
  33.  
  34. //--------------------------------------------------------------------------------------------------------
  35. function getBounds()
  36. {
  37. var alertBounds = Waze.map.getExtent();
  38.  
  39. alertBounds.transform(new OpenLayers.Projection("EPSG:900913"),new OpenLayers.Projection("EPSG:4326"));
  40. console.log('WME LMAO: Current bounds = Left ' + alertBounds.left + ', Right ' + alertBounds.right + ', Bottom ' + alertBounds.bottom + ', Top ' + alertBounds.top);//verify transform
  41. return alertBounds;
  42. }
  43.  
  44. //--------------------------------------------------------------------------------------------------------
  45. function getLiveMapAlerts(){
  46. LiveMapAlerts_Layer.destroyFeatures();
  47. var alertBounds = getBounds();
  48. var url = "/rtserver/web/GeoRSS";
  49. var data = {
  50. format: "JSON",
  51. types: "alerts",
  52. left: alertBounds.left,
  53. right: alertBounds.right,
  54. bottom: alertBounds.top,
  55. top: alertBounds.bottom
  56. };
  57. $.ajax({
  58. dataType: "json",
  59. url: url,
  60. data: data,
  61. success: function(json) {
  62. var alertData = json.alerts;
  63. try {
  64. for(var i=0; i<alertData.length; i++) {
  65. var lat = alertData[i].location.y;
  66. var long = alertData[i].location.x;
  67. var image = alertData[i].type;
  68. var title = alertData[i].reportDescription;
  69. console.log("WME LMAO: " + alertData[i].type, alertData[i].location.x, alertData[i].location.y);
  70. addImage(lat,long,image,alertData[i]);
  71. }
  72. }
  73. catch(e) {
  74. console.log('WME LMAO: No alerts in view');
  75. }
  76. }
  77. });
  78. }
  79.  
  80. //--------------------------------------------------------------------------------------------------------
  81. function addImage(lat, long, type, detail) {
  82. var coords = OpenLayers.Layer.SphericalMercator.forwardMercator(long, lat);
  83. var point = new OL.Geometry.Point(coords.lon,coords.lat);
  84. var alertPx = Waze.map.getPixelFromLonLat(new OpenLayers.LonLat(coords.lon,coords.lat));
  85. var imgRoot = '/assets';
  86. switch(type){
  87. case 'ROAD_CLOSED':
  88. var icon = '/livemap/alerts/road_closed-243aa9787857ee55f762c52ceb13813b.png';
  89. break;
  90. case 'ACCIDENT':
  91. var icon = '/livemap/alerts/accident-c5f76aaa426f2731d2e10c6ac99fe918.png';
  92. break;
  93. case 'JAM':
  94. var icon = '/livemap/alerts/jam-8269807a1e2a79c944a9577483ac8df7.png';
  95. break;
  96. case 'POLICEMAN': //Chrome
  97. var icon = '/livemap/alerts/police-503317f465b6295c7bf084ff773848e3.png';
  98. break;
  99. case 'POLICE': //Firefox
  100. var icon = '/livemap/alerts/police-503317f465b6295c7bf084ff773848e3.png';
  101. break;
  102. case 'HAZARD': //Firefox
  103. var icon = '/livemap/alerts/hazard-ed111f132551125f297b76ef4cca9101.png';
  104. break;
  105. case 'WEATHERHAZARD': //Chrome returns this for *all* hazards...
  106. var icon = '/livemap/alerts/hazard-ed111f132551125f297b76ef4cca9101.png';
  107. break;
  108. default:
  109. var icon = '/livemap/alerts/hazard-ed111f132551125f297b76ef4cca9101.png';
  110. };
  111. var attributes = {
  112. type: detail.type,
  113. subtype: detail.subtype,
  114. description: detail.reportDescription,
  115. street: detail.street,
  116. city: detail.city,
  117. near: detail.nearBy,
  118. reportby: detail.reportBy,
  119. pixel: alertPx
  120. };
  121. var style = {
  122. externalGraphic: imgRoot + icon,
  123. graphicWidth: 30,
  124. graphicHeight: 32,
  125. graphicZIndex: 9999,
  126. title: 'LiveMap',
  127. cursor: 'help'
  128. };
  129. var imageFeature = new OL.Feature.Vector(point, attributes, style);
  130. LiveMapAlerts_Layer.addFeatures([imageFeature]);
  131. //console.log('WME LMAO: Added alert at ' + lat + ',' + long);
  132.  
  133. }
  134.  
  135. //--------------------------------------------------------------------------------------------------------
  136. function initializeLiveMapAlerts()
  137. {
  138. console.log("WME LMAO: Initializing");
  139. var lmaoVisibility = true;
  140. // restore saved settings
  141. if (localStorage.WMELiveMapAlerts) {
  142. console.log("WME LMAO: loading options");
  143. var options = JSON.parse(localStorage.getItem("WMELiveMapAlerts"));
  144. lmaoVisibility = options[0];
  145. }
  146.  
  147. // overload the WME exit function
  148. saveLMAOOptions = function() {
  149. if (localStorage) {
  150. console.log("WME FC Highlights: saving options");
  151. var options = [];
  152. lmaoVisibility = LiveMapAlerts_Layer.visibility;
  153. options[0] = lmaoVisibility;
  154. localStorage.setItem("WMELiveMapAlerts", JSON.stringify(options));
  155. }
  156. }
  157. window.addEventListener("beforeunload", saveLMAOOptions, false);
  158. LiveMapAlerts_Layer = new OL.Layer.Vector("LiveMap Alerts",{
  159. rendererOptions: { zIndexing: true },
  160. uniqueName: 'livemap_alerts'
  161. }
  162. );
  163. function showAlertPopup(f){
  164. var attributes = f.attributes;
  165. var alertType = ((attributes.type == "WEATHERHAZARD") ? "HAZARD" : attributes.type);
  166. var alertSubType = ((attributes.subtype == null) ? "" : attributes.subtype);
  167. var alertDescription = ((attributes.description == null) ? "" : attributes.description);
  168. var alertStreet = ((attributes.street == null) ? "" : attributes.street);
  169. var alertCity = ((attributes.city == null) ? "" : attributes.city);
  170. var alertNear = ((attributes.near == null) ? "" : attributes.near);
  171. var alertReportBy = ((attributes.reportby == null) ? "" : attributes.reportby);
  172. var reportDetail = "<b>LiveMap Alert Details</b>"
  173. + "<br>TYPE: " + alertType
  174. + "<br>SUBTYPE: " + alertSubType
  175. + "<br>DESCRIPTION: " + alertDescription
  176. + "<br>STREET: " + alertStreet
  177. + "<br>CITY: " + alertCity
  178. + "<br>NEAR: " + alertNear
  179. + "<br>REPORT BY: " + alertReportBy
  180. ;
  181. document.getElementById("divLMAO").innerHTML = reportDetail;
  182. divLMAO.style.visibility = 'visible';
  183. };
  184. function hideAlertPopup(){
  185. divLMAO.style.visibility = 'hidden';
  186. };
  187.  
  188. LiveMapAlerts_Layer.setZIndex(9999);
  189. Waze.map.addLayer(LiveMapAlerts_Layer);
  190. Waze.map.addControl(new OL.Control.DrawFeature(LiveMapAlerts_Layer, OL.Handler.Path));
  191.  
  192. divLMAO = document.createElement('div');
  193. divLMAO.id = "divLMAO";
  194. divLMAO.style.position = 'absolute';
  195. divLMAO.style.visibility = 'hidden';
  196. divLMAO.style.top = '175px';
  197. divLMAO.style.left = '375px';
  198. divLMAO.style.zIndex = 1000;
  199. divLMAO.style.backgroundColor = 'aliceblue';
  200. divLMAO.style.borderWidth = '3px';
  201. divLMAO.style.borderStyle = 'ridge';
  202. divLMAO.style.borderRadius = '10px';
  203. divLMAO.style.boxShadow = '5px 5px 10px Silver';
  204. divLMAO.style.padding = '4px';
  205. document.body.appendChild(divLMAO);
  206. //clear existing LMAO features
  207. LiveMapAlerts_Layer.destroyFeatures();
  208. var lmaoLayer = null;
  209. for(i=0; i<Waze.map.layers.length; i++)
  210. {
  211. if(Waze.map.layers[i].uniqueName == 'livemap_alerts') lmaoLayer = i;
  212. }
  213. //console.log('WME LMAO: layer number = ' + lmaoLayer);
  214. Waze.map.events.register("mousemove", Waze.map, function(e) {
  215. hideAlertPopup();
  216. var position = this.events.getMousePosition(e);
  217. console.log('WME LMAO: coords xy = ' + position.x + ' ' + position.y);
  218. if(Waze.map.layers[lmaoLayer].features.length > 0){
  219.  
  220. var alertCount = Waze.map.layers[lmaoLayer].features.length;
  221. console.log('WME LMAO: Current LiveMap alert count = ' + alertCount);
  222. var alertFeatures = Waze.map.layers[lmaoLayer];
  223. for(j=0; j<Waze.map.layers[lmaoLayer].features.length; j++){
  224. var alertX = alertFeatures.features[j].attributes.pixel.x;
  225. var alertY = alertFeatures.features[j].attributes.pixel.y;
  226. if(position.x > alertX - 10 && position.x < alertX + 10 && position.y > alertY - 10 && position.y < alertY + 10){
  227. console.log('WME LMAO: hover over alert');
  228. showAlertPopup(alertFeatures.features[j]);
  229. }
  230. }
  231. }
  232. });
  233. window.setTimeout(getLiveMapAlerts(), 500);
  234. //window.setInterval(getLiveMapAlerts(), 60000); //refresh every minute
  235. //refresh if user moves map
  236. Waze.map.events.register("moveend", Waze.map, getLiveMapAlerts);
  237. window.setTimeout(getLiveMapAlerts(), 500);
  238.  
  239. }
  240.  
  241. //--------------------------------------------------------------------------------------------------------------
  242. bootstrapLiveMapAlerts();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址