WME Route Checker

Allows editors to check the route between two segments

当前为 2016-02-08 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name WME Route Checker
  3. // @namespace http://userscripts.org/users/419370
  4. // @description Allows editors to check the route between two segments
  5. // @include https://www.waze.com/editor/*
  6. // @include https://www.waze.com/*/editor/*
  7. // @include https://editor-beta.waze.com/*
  8. // @version 1.20
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. // globals
  13. var wmerc_version = "1.20";
  14.  
  15. var AVOID_TOLLS = 1;
  16. var AVOID_FREEWAYS = 2;
  17. var AVOID_DIRT = 4;
  18. var ALLOW_UTURNS = 16;
  19. var ROUTE_DIST = 32;
  20. var VEHICLE_TAXI = 64;
  21.  
  22. var route_options = ALLOW_UTURNS; // default
  23.  
  24. var routeZIndex = 0;
  25.  
  26. var routeColors = ["#8309e1", "#52BAD9", "#888800" ];
  27.  
  28.  
  29. function showRouteOptions() {
  30. if (Waze.selectionManager.selectedItems.length != 2) {
  31. WMERC_lineLayer_route.destroyFeatures();
  32. WMERC_lineLayer_route.setZIndex(-999);
  33. return;
  34. }
  35.  
  36. if (getId('routeOptions') !== null) {
  37. return;
  38. }
  39.  
  40. // hook into edit panel on the left
  41. var userTabs = getId('edit-panel');
  42. var segmentBox = getElementsByClassName('segment', userTabs)[0];
  43. var navTabs = getElementsByClassName('nav-tabs', segmentBox)[0];
  44. var routeTab = document.createElement('li');
  45. routeTab.innerHTML = '<a href="#route-checker" data-toggle="tab" id="route-tab-link">Routes</a>';
  46. navTabs.appendChild(routeTab);
  47. getId('route-tab-link').onclick = fetchRoute;
  48. var tabContent = getElementsByClassName('tab-content', segmentBox)[0];
  49.  
  50. var routeContent = document.createElement('div');
  51. routeContent.id = 'route-checker';
  52. routeContent.className = 'tab-pane';
  53. tabContent.appendChild(routeContent);
  54. // add new edit tab to left of the map
  55. var routeOptions = document.createElement('div');
  56. routeOptions.id = "routeOptions";
  57. routeOptions.style.borderTop = "solid 2px #E9E9E9";
  58. routeOptions.style.borderBottom = "solid 2px #E9E9E9";
  59. routeContent.appendChild(routeOptions);
  60. if (location.hostname.match(/editor.*.waze.com/)) {
  61. var coords1 = getCoords(Waze.selectionManager.selectedItems[0]);
  62. var coords2 = getCoords(Waze.selectionManager.selectedItems[1]);
  63. var url = getLivemap()
  64. + "&from_lon="+coords1.lon + "&from_lat="+coords1.lat
  65. + "&to_lon="+coords2.lon + "&to_lat="+coords2.lat
  66. + "&at_req=0&at_text=Now";
  67.  
  68. routeOptions.innerHTML = '<p><b><a href="'+url+'" title="Opens in new tab" target="LiveMap" style="color:#8309e1">Show routes in LiveMap</a> &raquo;</b></p>';
  69. } else {
  70. routeOptions.innerHTML = '<p><b><a href="#" id="goroutes" title="WME Route Checker v'+wmerc_version+'" style="color:#8309e1">'
  71. + 'Show routes between these 2 segments</a> &raquo;</b><br>'
  72. + '<b>Vehicle:</b>'
  73. + ' <input type="radio" name="_vehicleType" id="_vehicleType_private" value="0" checked> Private'
  74. + ' <input type="radio" name="_vehicleType" id="_vehicleType_taxi" value="1"> Taxi<br>'
  75. + '<b>Route:</b>'
  76. + ' <input type="radio" name="_routeType" id="_routeType_fast" value="0" checked> Fastest'
  77. + ' <input type="radio" name="_routeType" id="_routeType_short" value="1"> Shortest<br>'
  78. + '<b>Avoid:</b>'
  79. + ' <input type="checkbox" id="_avoidTolls" /> Tolls'
  80. + ' <input type="checkbox" id="_avoidFreeways" /> Freeways'
  81. + ' <input type="checkbox" id="_avoidDirt" /> Dirt Trails<br>'
  82. + '<b>Allow:</b>'
  83. + ' <input type="checkbox" id="_allowUTurns" /> U-Turns</p>';
  84.  
  85. getId('_avoidTolls').checked = route_options & AVOID_TOLLS;
  86. getId('_avoidFreeways').checked = route_options & AVOID_FREEWAYS;
  87. getId('_avoidDirt').checked = route_options & AVOID_DIRT;
  88. getId('_allowUTurns').checked = route_options & ALLOW_UTURNS;
  89. getId('_routeType_short').checked = route_options & ROUTE_DIST;
  90. getId('_vehicleType_taxi').checked = route_options & VEHICLE_TAXI;
  91. // automatically start getting route when user clicks on link
  92. getId('goroutes').onclick = fetchRoute;
  93. }
  94.  
  95. // create empty div ready for instructions
  96. var routeTest = document.createElement('div');
  97. routeTest.id = "routeTest";
  98. routeContent.appendChild(routeTest);
  99.  
  100. return;
  101. }
  102.  
  103. function saveOptions() {
  104. route_options = (getId('_avoidTolls').checked ? AVOID_TOLLS : 0)
  105. + (getId('_avoidFreeways').checked ? AVOID_FREEWAYS : 0)
  106. + (getId('_avoidDirt').checked ? AVOID_DIRT : 0)
  107. + (getId('_allowUTurns').checked ? ALLOW_UTURNS : 0)
  108. + (getId('_routeType_short').checked ? ROUTE_DIST : 0)
  109. + (getId('_vehicleType_taxi').checked ? VEHICLE_TAXI : 0);
  110.  
  111. console.log("WME Route Checker: saving options: " + route_options);
  112. localStorage.WMERouteChecker = JSON.stringify(route_options);
  113. }
  114.  
  115. function getOptions() {
  116. var list = 'AVOID_TOLL_ROADS' + (route_options & AVOID_TOLLS ? ':t' : ':f') + ','
  117. + 'AVOID_PRIMARIES' + (route_options & AVOID_FREEWAYS ? ':t' : ':f') + ','
  118. + 'AVOID_TRAILS' + (route_options & AVOID_DIRT ? ':t' : ':f') + ','
  119. + 'ALLOW_UTURNS' + (route_options & ALLOW_UTURNS ? ':t' : ':f');
  120. return list;
  121. }
  122.  
  123. function getCoords(segment) {
  124. var numpoints = segment.geometry.components.length;
  125. var middle = Math.floor(numpoints / 2);
  126.  
  127. var seglat, seglon;
  128. if (numpoints % 2 == 1 || numpoints < 2) { // odd number, middle point
  129. seglat = segment.geometry.components[middle].y;
  130. seglon = segment.geometry.components[middle].x;
  131. }
  132. else { // even number - take average of middle two points
  133. seglat = (segment.geometry.components[middle].y
  134. + segment.geometry.components[middle-1].y) / 2.0;
  135. seglon = (segment.geometry.components[middle].x
  136. + segment.geometry.components[middle-1].x) / 2.0;
  137. }
  138. return OpenLayers.Layer.SphericalMercator.inverseMercator(seglon,seglat);
  139. }
  140.  
  141. function fetchRoute(reverse) {
  142. saveOptions();
  143.  
  144. var coords1, coords2;
  145. reverse = (reverse !== false);
  146. if (reverse) {
  147. coords1 = getCoords(Waze.selectionManager.selectedItems[0]);
  148. coords2 = getCoords(Waze.selectionManager.selectedItems[1]);
  149. } else {
  150. coords1 = getCoords(Waze.selectionManager.selectedItems[1]);
  151. coords2 = getCoords(Waze.selectionManager.selectedItems[0]);
  152. }
  153.  
  154. var img = '<img src="https://www.waze.com/images/search_indicator.gif" hspace="4">';
  155.  
  156. // get the route, fix and parse the json
  157. getId('routeTest').innerHTML = "<p><b>Fetching route from LiveMap " + img + "</b></p>";
  158. var url = getRoutingManager();
  159. var data = {
  160. from: "x:" + coords1.lon + " y:" + coords1.lat + " bd:true",
  161. to: "x:" + coords2.lon + " y:" + coords2.lat + " bd:true",
  162. returnJSON: true,
  163. returnGeometries: true,
  164. returnInstructions: true,
  165. type: (route_options & ROUTE_DIST ? 'DISTANCE' : 'HISTORIC_TIME'),
  166. clientVersion: '4.0.0',
  167. timeout: 60000,
  168. nPaths: 3,
  169. options: getOptions()};
  170. if (route_options & VEHICLE_TAXI) {
  171. data.vehicleType = 'TAXI';
  172. }
  173.  
  174. $.ajax({
  175. dataType: "json",
  176. url: url,
  177. data: data,
  178. dataFilter: function(data, dataType) {
  179. return data.replace(/NaN/g, '0');
  180. },
  181. success: function(json) {
  182. showNavigation(json, reverse);
  183. }
  184. });
  185. return false;
  186. }
  187.  
  188. function getLivemap() {
  189. var center_lonlat=new OpenLayers.LonLat(Waze.map.center.lon,Waze.map.center.lat);
  190. center_lonlat.transform(new OpenLayers.Projection ("EPSG:900913"),new OpenLayers.Projection("EPSG:4326"));
  191. var coords = '?lon='+center_lonlat.lon+'&lat='+center_lonlat.lat;
  192.  
  193. return 'https://www.waze.com/livemap'+coords;
  194. }
  195.  
  196. function getRoutingManager() {
  197. if (Waze.model.countries.get(235) || Waze.model.countries.get(40)) { // US & Canada
  198. return '/RoutingManager/routingRequest';
  199. } else if (Waze.model.countries.get(106)) { // Israel
  200. return '/il-RoutingManager/routingRequest';
  201. } else {
  202. return '/row-RoutingManager/routingRequest';
  203. }
  204. }
  205.  
  206. function plotRoute(coords, index) {
  207. var points = [];
  208. for (var i in coords) {
  209. if (i > 0) {
  210. var point = OpenLayers.Layer.SphericalMercator.forwardMercator(coords[i].x, coords[i].y);
  211. points.push(new OL.Geometry.Point(point.lon,point.lat));
  212. }
  213. }
  214. var newline = new OL.Geometry.LineString(points);
  215.  
  216. var style = {
  217. strokeColor: routeColors[index],
  218. strokeOpacity: 0.7,
  219. strokeWidth: 8 - index * 2,
  220. };
  221. var lineFeature = new OL.Feature.Vector(newline, {type: "routeArrow"}, style);
  222.  
  223. // Display new segment
  224. WMERC_lineLayer_route.addFeatures([lineFeature]);
  225. }
  226.  
  227. function showNavigation(nav_json, reverse) {
  228. // plot the route
  229. WMERC_lineLayer_route.destroyFeatures();
  230. if (typeof nav_json.alternatives !== "undefined"){
  231. for (var r = nav_json.alternatives.length-1; r >= 0; r--) {
  232. plotRoute(nav_json.alternatives[r].coords, r);
  233. }
  234. } else {
  235. plotRoute(nav_json.coords, 0);
  236. }
  237. WMERC_lineLayer_route.setVisibility(true);
  238. WMERC_lineLayer_route.setZIndex(routeZIndex);
  239.  
  240. // hide segment details
  241. var userTabs = getId('edit-panel');
  242. var segmentBox = getElementsByClassName('segment', userTabs)[0];
  243. var tabContent = getElementsByClassName('tab-content', segmentBox)[0];
  244.  
  245. // write instructions
  246. instructions = getId('routeTest');
  247. instructions.innerHTML = '';
  248. instructions.style.display = 'block';
  249. instructions.style.height = document.getElementById('map').style.height;
  250.  
  251. var nav_coords;
  252. if (typeof nav_json.alternatives !== "undefined") {
  253. for (var r = 0; r < nav_json.alternatives.length; r++) {
  254. showInstructions(nav_json.alternatives[r], r);
  255. }
  256. nav_coords = nav_json.alternatives[0].coords;
  257. } else {
  258. showInstructions(nav_json, 0);
  259. nav_coords = nav_json.coords;
  260. }
  261. var lon1 = nav_coords[0].x;
  262. var lat1 = nav_coords[0].y;
  263.  
  264. var end = nav_coords.length - 1;
  265. var lon2 = nav_coords[end].x;
  266. var lat2 = nav_coords[end].y;
  267.  
  268. var rerouteArgs = '{lon:'+lon1+',lat:'+lat1+'},{lon:'+lon2+',lat:'+lat2+'}';
  269.  
  270. // footer for extra links
  271. var footer = document.createElement('div');
  272. footer.className = 'routes_footer';
  273.  
  274. // create link to reverse the route
  275. var reverseLink = document.createElement('a');
  276. reverseLink.innerHTML = '&laquo; Reverse Route';
  277. reverseLink.href = '#';
  278. reverseLink.setAttribute('onClick', 'fetchRoute('+!reverse+');');
  279. reverseLink.addEventListener('click', function() { fetchRoute(!reverse); }, false);
  280. footer.appendChild(reverseLink);
  281.  
  282. footer.appendChild(document.createTextNode(' | '));
  283.  
  284. var url = getLivemap()
  285. + "&from_lon="+lon1 + "&from_lat="+lat1
  286. + "&to_lon="+lon2 + "&to_lat="+lat2
  287. + "&at_req=0&at_text=Now";
  288.  
  289. // create link to view the navigation instructions
  290. var livemapLink = document.createElement('a');
  291. livemapLink.innerHTML = 'View in LiveMap &raquo;';
  292. livemapLink.href = url;
  293. livemapLink.target="LiveMap";
  294. footer.appendChild(livemapLink);
  295.  
  296. footer.appendChild(document.createElement('br'));
  297.  
  298. // add link to script homepage and version
  299. var scriptLink = document.createElement('a');
  300. scriptLink.innerHTML = 'WME Route Checker v' + wmerc_version;
  301. scriptLink.href = 'https://www.waze.com/forum/viewtopic.php?t=64777';
  302. scriptLink.style.fontStyle = 'italic';
  303. scriptLink.target="_blank";
  304. footer.appendChild(scriptLink);
  305.  
  306. instructions.appendChild(footer);
  307.  
  308. return false;
  309. }
  310.  
  311. function showInstructions(nav_json, r) {
  312. // for each route returned by Waze...
  313. var route = nav_json.response;
  314. var streetNames = route.streetNames;
  315.  
  316. if (r > 0) { // divider
  317. instructions.appendChild(document.createElement('p'));
  318. }
  319.  
  320. // name of the route, with coloured icon
  321. var route_name = document.createElement('p');
  322. route_name.className = 'route';
  323. route_name.style.borderColor = routeColors[r];
  324. route_name.innerHTML = '<b style="color:'+routeColors[r]+'">Via ' + route.routeName + '</b>';
  325. instructions.appendChild(route_name);
  326.  
  327. if (route.tollMeters > 0) {
  328. route_name.innerHTML = '<span style="float: right">TOLL</span>' + route_name.innerHTML;
  329. }
  330.  
  331. var optail = '';
  332. var prevStreet = '';
  333. var currentItem = null;
  334. var totalDist = 0;
  335. var totalTime = 0;
  336. //var detourSaving = 0;
  337. // street name at starting point
  338. var streetName = streetNames[route.results[0].street];
  339. var departFrom = 'depart';
  340. if (!streetName || streetName === null)
  341. streetName = '';
  342. else {
  343. departFrom = 'depart from ' + streetName;
  344. streetName = ' from <span style="color: blue">' + streetName + '<span>';
  345. }
  346. // turn icon at starting coordinates
  347. if (r == 0) {
  348. addTurnImageToMap(nav_json.coords[0], "big_direction_forward", departFrom);
  349. }
  350.  
  351. // add first instruction (depart)
  352. currentItem = document.createElement('a');
  353. currentItem.className = 'step';
  354. currentItem.innerHTML = getTurnImageSrc('big_direction_forward') + 'depart' + streetName;
  355. instructions.appendChild(currentItem);
  356.  
  357. // iterate over all the steps in the list
  358. for (var i = 0; i < route.results.length; i++) {
  359. totalDist += route.results[i].length;
  360. totalTime += route.results[i].crossTime;
  361. //detourSaving += route.results[i].detourSavings;
  362.  
  363. if (!route.results[i].instruction)
  364. continue;
  365. var opcode = route.results[i].instruction.opcode;
  366. if (!opcode)
  367. continue;
  368.  
  369. // ignore these
  370. if (opcode.match(/ROUNDABOUT_EXIT|CONTINUE|NONE/)) {
  371. continue;
  372. }
  373.  
  374. // the image for the turn
  375. var dirImage = getTurnImage(opcode);
  376. var dirImageSrc = '';
  377. if (dirImage !== '') {
  378. dirImageSrc = '';
  379. }
  380. // the name that TTS will read out (in blue)
  381. streetName = getNextStreetName(route.results, i, route.streetNames);
  382.  
  383. // roundabouts with nth exit instructions
  384. if (opcode == 'ROUNDABOUT_ENTER') {
  385. opcode += route.results[i].instruction.arg + 'th exit';
  386. opcode = opcode.replace(/1th/, '1st');
  387. opcode = opcode.replace(/2th/, '2nd');
  388. opcode = opcode.replace(/3th/, '3rd');
  389. }
  390.  
  391. // convert opcode to pretty text
  392. opcode = opcode.replace(/APPROACHING_DESTINATION/, 'arrive');
  393. opcode = opcode.replace(/ROUNDABOUT_(EXIT_)?LEFT/, 'at the roundabout, turn left');
  394. opcode = opcode.replace(/ROUNDABOUT_(EXIT_)?RIGHT/, 'at the roundabout, turn right');
  395. opcode = opcode.replace(/ROUNDABOUT_(EXIT_)?STRAIGHT/, 'at the roundabout, continue straight');
  396. opcode = opcode.replace(/ROUNDABOUT_ENTER/, 'at the roundabout, take ');
  397. opcode = opcode.toLowerCase().replace(/_/, ' ');
  398. opcode = opcode.replace(/uturn/, 'make a U-turn');
  399. opcode = opcode.replace(/roundabout u/, 'at the roundabout, make a U-turn');
  400.  
  401. // convert keep to exit if needed
  402. var keepSide = Waze.model.isLeftHand ? /keep left/ : /keep right/;
  403. if (opcode.match(keepSide) && i+1 < route.results.length &&
  404. isKeepForExit(route.results[i].roadType, route.results[i+1].roadType)) {
  405. opcode = opcode.replace(/keep (.*)/, 'exit $1');
  406. }
  407.  
  408. // show turn symbol on the map (for first route only)
  409. if (r == 0) {
  410. if (opcode == 'arrive') {
  411. var end = nav_json.coords.length - 1;
  412. var title = 'arrive at ' + (streetName != '' ? streetName : 'destination');
  413. addTurnImageToMap(nav_json.coords[end], dirImage, title);
  414. } else {
  415. //var title = opcode + (streetName != '' ? ' onto ' + streetName : '');
  416. var title = opcode.replace(/at the roundabout, /, '');
  417. if (streetName != '') title += ' onto ' + streetName;
  418. addTurnImageToMap(route.results[i+1].path, dirImage, title);
  419. }
  420. }
  421.  
  422. // pretty street name
  423. if (streetName != '') {
  424. if (opcode == 'arrive') {
  425. streetName = ' at <span style="color: blue">' + streetName + '</span>';
  426. } else {
  427. streetName = ' onto <span style="color: blue">' + streetName + '</span>';
  428. }
  429. }
  430. // display new instruction
  431. currentItem = document.createElement('a');
  432. currentItem.className = 'step';
  433. currentItem.innerHTML = getTurnImageSrc(dirImage) + opcode + streetName;
  434. if (opcode.match(/0th exit/)) {
  435. currentItem.style.color = 'red';
  436. }
  437. instructions.appendChild(currentItem);
  438. }
  439.  
  440. // append distance and time to last instruction
  441. currentItem.title = (totalDist/1609).toFixed(3) + " miles";
  442. currentItem.innerHTML += ' - ' + totalDist/1000 + ' km';
  443. currentItem.innerHTML += ' - ' + timeFromSecs(totalTime);
  444. //if (detourSaving > 0) {
  445. // currentItem.innerHTML += '<br>&nbsp; <i>detour saved ' + timeFromSecs(detourSaving) + '</i>';
  446. //}
  447. };
  448.  
  449. function getNextStreetName(results, index, streetNames) {
  450. var streetName = '';
  451. var unnamedCount = 0;
  452. var unnamedLength = 0;
  453.  
  454. // destination
  455. if (index == results.length-1) {
  456. streetName = streetNames[results[index].street];
  457. if (!streetName || streetName === null)
  458. streetName = '';
  459. }
  460. // look ahead to next street name
  461. while (++index < results.length && streetName == '') {
  462. streetName = streetNames[results[index].street];
  463. if (!streetName || streetName === null)
  464. streetName = '';
  465.  
  466. // "Navigation instructions for unnamed segments" <- in the Wiki
  467. if (streetName == '' && !isFreewayOrRamp(results[index].roadType)
  468. && !isRoundabout(results[index].path.segmentId)) {
  469. unnamedLength += length;
  470. unnamedCount++;
  471. if (unnamedCount >= 4 || unnamedLength >= 400) {
  472. //console.log("- unnamed segments too long; break");
  473. break;
  474. }
  475. }
  476. }
  477. return streetName;
  478. }
  479.  
  480. function getTurnImage(opcode) {
  481. var dirImage = '';
  482. switch (opcode) {
  483. case "CONTINUE":
  484. case "NONE": dirImage = "big_direction_forward"; break;
  485. case "TURN_LEFT": dirImage = "big_direction_left"; break;
  486. case "TURN_RIGHT": dirImage = "big_direction_right"; break;
  487. case "KEEP_LEFT":
  488. case "EXIT_LEFT": dirImage = "big_direction_exit_left"; break;
  489. case "KEEP_RIGHT":
  490. case "EXIT_RIGHT": dirImage = "big_direction_exit_right"; break;
  491. case "UTURN": dirImage = "big_directions_roundabout_u"; break;
  492. case "APPROACHING_DESTINATION": dirImage = "big_direction_end"; break;
  493. case "ROUNDABOUT_LEFT":
  494. case "ROUNDABOUT_EXIT_LEFT": dirImage = "big_directions_roundabout_l"; break;
  495. case "ROUNDABOUT_RIGHT":
  496. case "ROUNDABOUT_EXIT_RIGHT": dirImage = "big_directions_roundabout_r"; break;
  497. case "ROUNDABOUT_STRAIGHT":
  498. case "ROUNDABOUT_EXIT_STRAIGHT": dirImage = "big_directions_roundabout_s"; break;
  499. case "ROUNDABOUT_ENTER":
  500. case "ROUNDABOUT_EXIT": dirImage = "big_directions_roundabout"; break;
  501. case "ROUNDABOUT_U": dirImage = "big_directions_roundabout_u"; break;
  502. default: dirImage = '';
  503. }
  504. return dirImage;
  505. }
  506.  
  507. function getTurnImageSrc(dirImage) {
  508. var imgRoot = '/assets-editor/images/vectors/routeInstructions/';
  509. if (dirImage != '') {
  510. return '<img src="' + imgRoot + dirImage + '.png" style="float: left; top: 0; padding-right: 4px" width="16" height="16" />';
  511. }
  512. return '';
  513. }
  514.  
  515. function isKeepForExit(fromType, toType) {
  516. // primary to non-primary
  517. if (isPrimaryRoad(fromType) && !isPrimaryRoad(toType)) {
  518. return true;
  519. }
  520. // ramp to non-primary or non-ramp
  521. if (isRamp(fromType) && !isPrimaryRoad(toType) && !isRamp(toType)) {
  522. return true;
  523. }
  524. return false;
  525. }
  526.  
  527. function isFreewayOrRamp(t) {
  528. return t === 3 /*FREEWAY*/ || t === 4 /*RAMP*/;
  529. }
  530.  
  531. function isPrimaryRoad(t) {
  532. return t === 3 /*FREEWAY*/ || t === 6 /*MAJOR_HIGHWAY*/ || t === 7 /*MINOR_HIGHWAY*/;
  533. }
  534.  
  535. function isRamp(t) {
  536. return t === 4 /*RAMP*/;
  537. }
  538.  
  539. function isRoundabout(id) {
  540. segment = Waze.model.segments.get(id);
  541. if (typeof segment != "undefined") {
  542. return segment.attributes.junctionId !== null;
  543. }
  544. return false;
  545. }
  546.  
  547. function timeFromSecs(seconds)
  548. {
  549. var hh = '00'+Math.floor(((seconds/86400)%1)*24);
  550. var mm = '00'+Math.floor(((seconds/3600)%1)*60);
  551. var ss = '00'+Math.round(((seconds/60)%1)*60);
  552. return hh.slice(-2) + ':' + mm.slice(-2) + ':' + ss.slice(-2);
  553. }
  554.  
  555. function addTurnImageToMap(location, image, title) {
  556. if (image == '') return;
  557. var coords = OpenLayers.Layer.SphericalMercator.forwardMercator(location.x, location.y);
  558. var point = new OL.Geometry.Point(coords.lon,coords.lat);
  559. var imgRoot = '/assets-editor/images/vectors/routeInstructions/';
  560.  
  561. var style = {
  562. externalGraphic: imgRoot + image + ".png",
  563. graphicWidth: 30,
  564. graphicHeight: 32,
  565. graphicZIndex: 9999,
  566. label: title,
  567. labelXOffset: 20,
  568. labelAlign: 'l',
  569. labelOutlineColor: 'white',
  570. labelOutlineWidth: 3,
  571. fontWeight: 'bold',
  572. fontColor: routeColors[0]
  573. };
  574. if (title.match(/0th exit/)) {
  575. style.fontColor = 'red';
  576. }
  577.  
  578. var imageFeature = new OL.Feature.Vector(point, null, style);
  579. // Display new segment
  580. WMERC_lineLayer_route.addFeatures([imageFeature]);
  581. }
  582.  
  583. /* helper function */
  584. function getElementsByClassName(classname, node) {
  585. if(!node) node = document.getElementsByTagName("body")[0];
  586. var a = [];
  587. var re = new RegExp('\\b' + classname + '\\b');
  588. var els = node.getElementsByTagName("*");
  589. for (var i=0,j=els.length; i<j; i++)
  590. if (re.test(els[i].className)) a.push(els[i]);
  591. return a;
  592. }
  593.  
  594. function getId(node) {
  595. return document.getElementById(node);
  596. }
  597.  
  598. function initialiseRouteChecker() {
  599. if (typeof Waze == 'undefined') {
  600. return; // not WME
  601. }
  602.  
  603. console.log("WME Route Checker: initialising v" + wmerc_version);
  604. if (localStorage.WMERouteChecker) {
  605. route_options = JSON.parse(localStorage.WMERouteChecker);
  606. console.log("WME Route Checker: loaded options: " + route_options);
  607. }
  608.  
  609. /* dirty hack to inject stylesheet in to the DOM */
  610. var style = document.createElement('style');
  611. style.innerHTML = "#routeTest {padding: 0 4px 0 0; overflow-y: auto;}\n"
  612. + "#routeTest p.route {margin: 0; padding: 4px 8px; border-bottom: silver solid 3px; background: #eee}\n"
  613. + "#routeTest a.step {display: block; margin: 0; padding: 3px 8px; text-decoration: none; color:black;border-bottom: silver solid 1px;}\n"
  614. + "#routeTest a.step:hover {background: #ffd;}\n"
  615. + "#routeTest a.step:active {background: #dfd;}\n"
  616. + "#routeTest div.routes_footer {text-align: center; margin-bottom: 25px;}\n";
  617. (document.body || document.head || document.documentElement).appendChild(style);
  618.  
  619. // add a new layer for routes
  620. WMERC_lineLayer_route = new OL.Layer.Vector("Route Checker Script",
  621. { rendererOptions: { zIndexing: true },
  622. shortcutKey: "S+t",
  623. uniqueName: 'route_checker' }
  624. );
  625. Waze.map.addLayer(WMERC_lineLayer_route);
  626. Waze.map.addControl(new OL.Control.DrawFeature(WMERC_lineLayer_route, OL.Handler.Path));
  627.  
  628. // find best ZIndex
  629. for (var i = 0; i < Waze.map.layers.length; i++) {
  630. var l = Waze.map.layers[i];
  631. if (/Waze.Control.SelectHighlightFeature/.test(l.name)) {
  632. routeZIndex = l.getZIndex()+1;
  633. }
  634. }
  635.  
  636. // hack in translation:
  637. I18n.translations[I18n.locale].layers.name.route_checker = "Route Checker Script";
  638.  
  639. // ...and then hide it
  640. $("label:contains('Route Checker Script')").parent().remove();
  641.  
  642. // add listener for whenever selection changes
  643. Waze.selectionManager.events.register("selectionchanged", null, showRouteOptions);
  644.  
  645. showRouteOptions(); // for permalinks
  646. }
  647.  
  648. // bootstrap!
  649. (function()
  650. {
  651. setTimeout(initialiseRouteChecker, 1003);
  652. })();
  653.  
  654. /* end ======================================================================= */