WME Route Checker

Allows editors to check the route between two segments

当前为 2016-04-17 提交的版本,查看 最新版本

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