From: Dan Stowell Date: Sun, 9 Mar 2014 09:37:42 +0000 (+0000) Subject: jsrouting: mouseover an instruction, highlight the route segment X-Git-Tag: live~4217^2~22^2 X-Git-Url: https://git.openstreetmap.org/rails.git/commitdiff_plain/15f0367e7fb8bf78059759414d7ec0d3dcceb42a jsrouting: mouseover an instruction, highlight the route segment --- diff --git a/app/assets/javascripts/routing.js.erb b/app/assets/javascripts/routing.js.erb index d62e94f0c..7bb27f72b 100644 --- a/app/assets/javascripts/routing.js.erb +++ b/app/assets/javascripts/routing.js.erb @@ -10,6 +10,12 @@ var ROUTING_POLYLINE={ weight: 10 }; +var ROUTING_POLYLINE_HIGHLIGHT={ + color: '#ff0', + opacity: 0.5, + weight: 12 +}; + OSM.RoutingEngines={ list: [] @@ -171,7 +177,7 @@ OSM.Routing=function(map,name,jqSearch) { }; // Take directions and write them out - // data = { steps: array of [latlng, sprite number, instruction text, distance in metres] } + // data = { steps: array of [latlng, sprite number, instruction text, distance in metres, highlightPolyline] } // sprite numbers equate to OSRM's route_instructions turn values r.setItinerary=function(data) { // Create base table @@ -200,8 +206,9 @@ OSM.Routing=function(map,name,jqSearch) { row.append(" "); row.append(""+step[2]); row.append(""+dist); - with ({ num: i, ll: step[0] }) { + with ({ num: i, ll: step[0], lineseg: step[4] }) { row.on('click',function(e) { r.clickTurn(num, ll); }); + row.hover(function(e){r.highlightSegment(lineseg);}, function(e){r.unhighlightSegment();}); }; $('#turnbyturn').append(row); cumulative+=step[3]; @@ -212,6 +219,13 @@ OSM.Routing=function(map,name,jqSearch) { r.clickTurn=function(num,latlng) { r.popup=L.popup().setLatLng(latlng).setContent("

"+(num+1)+"

").openOn(r.map); }; + r.highlightSegment=function(lineseg){ + if (r.highlighted) map.removeLayer(r.highlighted); + r.highlighted=L.polyline(lineseg, ROUTING_POLYLINE_HIGHLIGHT).addTo(r.map); + } + r.unhighlightSegment=function(){ + if (r.highlighted) map.removeLayer(r.highlighted); + } r.formatDistance=function(m) { if (m < 1000 ) { return Math.round(m) + "m"; } else if (m < 10000) { return (m/1000.0).toFixed(1) + "km"; } diff --git a/app/assets/javascripts/routing_engines/graphhopper.js b/app/assets/javascripts/routing_engines/graphhopper.js index 5c10433c2..5e14ea0df 100644 --- a/app/assets/javascripts/routing_engines/graphhopper.js +++ b/app/assets/javascripts/routing_engines/graphhopper.js @@ -42,7 +42,7 @@ GraphHopperEngine.prototype.createConfig = function() { instrText += instr.descriptions[i]; var latlng = instr.latLngs[i]; var distInMeter = instr.distances[i]; - steps.push([{lat: latlng[0], lng: latlng[1]}, instrCode, instrText, distInMeter]); + steps.push([{lat: latlng[0], lng: latlng[1]}, instrCode, instrText, distInMeter, []]); // TODO does graphhopper map instructions onto line indices? } router.setItinerary({ steps: steps, distance: data.route.distance, time: data.route['time']/1000 }); return true; diff --git a/app/assets/javascripts/routing_engines/mapquest.js b/app/assets/javascripts/routing_engines/mapquest.js index eb92893b3..fd17e5942 100644 --- a/app/assets/javascripts/routing_engines/mapquest.js +++ b/app/assets/javascripts/routing_engines/mapquest.js @@ -60,14 +60,27 @@ MapQuestEngine.prototype.createConfig = function() { } router.setPolyline(poly); - // data.shape.maneuverIndexes links turns to polyline positions - // data.legs[0].maneuvers is list of turns + // data.route.shape.maneuverIndexes links turns to polyline positions + // data.route.legs[0].maneuvers is list of turns var steps=[]; var mq=data.route.legs[0].maneuvers; for (var i=0; i "; instText+=TURN_INSTRUCTIONS[instCodes[0]]; if (instCodes[1]) { instText+="exit "+instCodes[1]+" "; } if (instCodes[0]!=15) { instText+=s[1] ? ""+s[1]+"" : I18n.t('javascripts.directions.instructions.unnamed'); } - steps.push([line[s[3]], s[0].split('-')[0], instText, s[2]]); + if ((i+1)