finalized alternative integration
This commit is contained in:
@@ -54,9 +54,11 @@ timeoutRoute_Reversed: function() {
|
||||
OSRM.G.markers.reverseMarkers();
|
||||
OSRM.Routing.timeoutRoute();
|
||||
},
|
||||
showRoute: function(response) {
|
||||
showRoute: function(response, parameters) {
|
||||
if(!response)
|
||||
return;
|
||||
if(!parameters || parameters.keepAlternative == false)
|
||||
OSRM.G.active_alternative = 0;
|
||||
|
||||
OSRM.G.response = response; // needed for printing & history routes!
|
||||
if(response.status == 207) {
|
||||
@@ -65,11 +67,10 @@ showRoute: function(response) {
|
||||
OSRM.RoutingDescription.showNA( OSRM.loc("NO_ROUTE_FOUND") );
|
||||
OSRM.Routing._snapRoute();
|
||||
} else {
|
||||
OSRM.RoutingAlternatives.init(OSRM.G.response);
|
||||
OSRM.RoutingAlternatives.prepare(OSRM.G.response);
|
||||
OSRM.RoutingGeometry.show(OSRM.G.response);
|
||||
OSRM.RoutingNoNames.show(OSRM.G.response);
|
||||
OSRM.RoutingDescription.show(OSRM.G.response);
|
||||
OSRM.RoutingAlternatives._drawGUI();
|
||||
OSRM.Routing._snapRoute();
|
||||
}
|
||||
OSRM.Routing._updateHints(response);
|
||||
@@ -93,13 +94,15 @@ showRoute_Dragging: function(response) {
|
||||
if(OSRM.G.pending)
|
||||
setTimeout(OSRM.Routing.draggingTimeout,1);
|
||||
},
|
||||
showRoute_Redraw: function(response) {
|
||||
showRoute_Redraw: function(response, parameters) {
|
||||
if(!response)
|
||||
return;
|
||||
if(!parameters || parameters.keepAlternative == false)
|
||||
OSRM.G.active_alternative = 0;
|
||||
|
||||
OSRM.G.response = response; // not needed, even harmful as important information is not stored! ????
|
||||
OSRM.G.response = response; // not needed, even harmful as important information is not stored! ==> really ????
|
||||
if(response.status != 207) {
|
||||
OSRM.RoutingAlternatives.init(OSRM.G.response);
|
||||
OSRM.RoutingAlternatives.prepare(OSRM.G.response);
|
||||
OSRM.RoutingGeometry.show(OSRM.G.response);
|
||||
OSRM.RoutingNoNames.show(OSRM.G.response);
|
||||
}
|
||||
@@ -110,7 +113,7 @@ showRoute_Redraw: function(response) {
|
||||
//-- main function --
|
||||
|
||||
//generate server calls to query routes
|
||||
getRoute: function() {
|
||||
getRoute: function(parameters) {
|
||||
|
||||
// if source or target are not set -> hide route
|
||||
if( OSRM.G.markers.route.length < 2 ) {
|
||||
@@ -120,7 +123,7 @@ getRoute: function() {
|
||||
OSRM.JSONP.clear('dragging');
|
||||
OSRM.JSONP.clear('redraw');
|
||||
OSRM.JSONP.clear('route');
|
||||
OSRM.JSONP.call(OSRM.Routing._buildCall()+'&instructions=true', OSRM.Routing.showRoute, OSRM.Routing.timeoutRoute, OSRM.DEFAULTS.JSONP_TIMEOUT, 'route');
|
||||
OSRM.JSONP.call(OSRM.Routing._buildCall()+'&instructions=true', OSRM.Routing.showRoute, OSRM.Routing.timeoutRoute, OSRM.DEFAULTS.JSONP_TIMEOUT, 'route',parameters);
|
||||
},
|
||||
getRoute_Reversed: function() {
|
||||
if( OSRM.G.markers.route.length < 2 )
|
||||
@@ -131,13 +134,13 @@ getRoute_Reversed: function() {
|
||||
OSRM.JSONP.clear('route');
|
||||
OSRM.JSONP.call(OSRM.Routing._buildCall()+'&instructions=true', OSRM.Routing.showRoute, OSRM.Routing.timeoutRoute_Reversed, OSRM.DEFAULTS.JSONP_TIMEOUT, 'route');
|
||||
},
|
||||
getRoute_Redraw: function() {
|
||||
getRoute_Redraw: function(parameters) {
|
||||
if( OSRM.G.markers.route.length < 2 )
|
||||
return;
|
||||
|
||||
OSRM.JSONP.clear('dragging');
|
||||
OSRM.JSONP.clear('redraw');
|
||||
OSRM.JSONP.call(OSRM.Routing._buildCall()+'&instructions=true', OSRM.Routing.showRoute_Redraw, OSRM.Routing.timeoutRoute, OSRM.DEFAULTS.JSONP_TIMEOUT, 'redraw');
|
||||
OSRM.JSONP.call(OSRM.Routing._buildCall()+'&instructions=true', OSRM.Routing.showRoute_Redraw, OSRM.Routing.timeoutRoute, OSRM.DEFAULTS.JSONP_TIMEOUT, 'redraw',parameters);
|
||||
},
|
||||
getRoute_Dragging: function() {
|
||||
OSRM.G.pending = !OSRM.JSONP.call(OSRM.Routing._buildCall()+'&instructions=false', OSRM.Routing.showRoute_Dragging, OSRM.Routing.timeoutRoute_Dragging, OSRM.DEFAULTS.JSONP_TIMEOUT, 'dragging');;
|
||||
|
||||
@@ -21,23 +21,20 @@ or see http://www.gnu.org/licenses/agpl.txt.
|
||||
|
||||
OSRM.RoutingAlternatives = {
|
||||
|
||||
// remaining problems: how to handle PRINTING (do it internally), LINKS (add parameter to JSONP call)
|
||||
|
||||
// data of gui buttons for alternativess
|
||||
_buttons: [
|
||||
{id:"gui-a", label:"A"},
|
||||
{id:"gui-b", label:"B"}
|
||||
],
|
||||
|
||||
|
||||
// reset stored values
|
||||
reset: function() {
|
||||
// initialize required values
|
||||
init: function() {
|
||||
OSRM.G.active_alternative = 0;
|
||||
OSRM.G.alternative_count = 0;
|
||||
},
|
||||
|
||||
// restructure response data
|
||||
init: function(response) {
|
||||
prepare: function(response) {
|
||||
// move best route to alternative array
|
||||
var the_response = OSRM.G.response;
|
||||
the_response.alternative_geometries.unshift( response.route_geometry );
|
||||
@@ -46,9 +43,7 @@ init: function(response) {
|
||||
|
||||
// update basic information
|
||||
OSRM.G.alternative_count = response.alternative_geometries.length;
|
||||
if( OSRM.G.active_alternative == undefined )
|
||||
OSRM.G.active_alternative = 0;
|
||||
if( OSRM.G.active_alternative > OSRM.G.alternative_count - 1 )
|
||||
if( OSRM.G.active_alternative >= OSRM.G.alternative_count ) // reset if the selected alternative cannot be found
|
||||
OSRM.G.active_alternative = 0;
|
||||
|
||||
// switch data
|
||||
@@ -57,46 +52,6 @@ init: function(response) {
|
||||
the_response.route_summary = the_response.alternative_summaries[OSRM.G.active_alternative];
|
||||
},
|
||||
|
||||
// prepare using alternatives
|
||||
prepare: function(response) {
|
||||
// store basic information
|
||||
OSRM.G.alternative_count = response.alternative_geometries.length + 1;
|
||||
OSRM.G.active_alternative = 0;
|
||||
|
||||
// do nothing if there is no alternative
|
||||
// if( OSRM.G.alternative_count == 1 )
|
||||
// return;
|
||||
|
||||
// move best route to alternative array
|
||||
var the_response = OSRM.G.response;
|
||||
the_response.alternative_geometries.unshift( response.route_geometry );
|
||||
the_response.alternative_instructions.unshift( response.route_instructions );
|
||||
the_response.alternative_summaries.unshift( response.route_summary );
|
||||
|
||||
// add alternative GUI
|
||||
OSRM.RoutingAlternatives._drawGUI();
|
||||
},
|
||||
prepare_Redraw: function(response) {
|
||||
// do nothing if there is no alternative
|
||||
if( OSRM.G.alternative_count == 1 )
|
||||
return;
|
||||
|
||||
// move results
|
||||
var the_response = OSRM.G.response;
|
||||
the_response.alternative_geometries = response.alternative_geometries;
|
||||
the_response.alternative_instructions = response.alternative_instructions;
|
||||
the_response.alternative_summaries = response.alternative_summaries;
|
||||
|
||||
the_response.alternative_geometries.unshift( response.route_geometry );
|
||||
the_response.alternative_instructions.unshift( response.route_instructions );
|
||||
the_response.alternative_summaries.unshift( response.route_summary );
|
||||
|
||||
// switch data
|
||||
the_response.route_geometry = the_response.alternative_geometries[OSRM.G.active_alternative];
|
||||
the_response.route_instructions = the_response.alternative_instructions[OSRM.G.active_alternative];
|
||||
the_response.route_summary = the_response.alternative_summaries[OSRM.G.active_alternative];
|
||||
},
|
||||
|
||||
// switch active alternative and redraw buttons accordingly
|
||||
setActive: function(button_id) {
|
||||
// switch active alternative
|
||||
@@ -110,6 +65,33 @@ setActive: function(button_id) {
|
||||
// do nothing for non-clickable buttons
|
||||
},
|
||||
|
||||
//draw GUI and register click events
|
||||
show: function() {
|
||||
var buttons = OSRM.RoutingAlternatives._buttons;
|
||||
var data = "";
|
||||
// draw clickable buttons
|
||||
for(var i=0, size=OSRM.G.alternative_count; i<size; i++) {
|
||||
var distance = OSRM.Utils.toHumanDistance(OSRM.G.response.alternative_summaries[i].total_distance);
|
||||
var time = OSRM.Utils.toHumanTime(OSRM.G.response.alternative_summaries[i].total_time);
|
||||
var tooltip = OSRM.loc("DISTANCE")+":"+distance+" "+OSRM.loc("DURATION")+":"+time;
|
||||
var buttonClass = (i == OSRM.G.active_alternative) ? "button-pressed" : "button";
|
||||
data = '<a class="'+buttonClass+' top-right-button" id="'+buttons[i].id+'" title="'+tooltip+'">'+buttons[i].label+'</a>' + data;
|
||||
}
|
||||
// draw non-clickable buttons
|
||||
for(var i=OSRM.G.alternative_count, size=buttons.length; i<size; ++i) {
|
||||
data = '<a class="button-inactive top-right-button" id="'+buttons[i].id+'">'+buttons[i].label+'</a>' + data;
|
||||
}
|
||||
// add buttons to DOM
|
||||
document.getElementById('information-box-header').innerHTML = data + document.getElementById('information-box-header').innerHTML;
|
||||
|
||||
// add events
|
||||
for(var i=0, size=OSRM.G.alternative_count; i<size; i++) {
|
||||
document.getElementById(buttons[i].id).onclick = function (button_id) { return function() {OSRM.RoutingAlternatives._click(button_id); }; }(i) ;
|
||||
document.getElementById(buttons[i].id).onmouseover = function (button_id) { return function() {OSRM.RoutingAlternatives._mouseover(button_id); }; } (i);
|
||||
document.getElementById(buttons[i].id).onmouseout = function (button_id) { return function() {OSRM.RoutingAlternatives._mouseout(button_id); }; } (i);
|
||||
}
|
||||
},
|
||||
|
||||
// mouse events on buttons
|
||||
_click: function(button_id) {
|
||||
if( OSRM.G.active_alternative == button_id )
|
||||
@@ -123,11 +105,10 @@ _click: function(button_id) {
|
||||
the_response.route_instructions = the_response.alternative_instructions[button_id];
|
||||
the_response.route_summary = the_response.alternative_summaries[button_id];
|
||||
|
||||
// show alternative
|
||||
// redraw route & data
|
||||
OSRM.RoutingGeometry.show(the_response);
|
||||
OSRM.RoutingNoNames.show(the_response);
|
||||
OSRM.RoutingDescription.show(the_response);
|
||||
OSRM.RoutingAlternatives._drawGUI();
|
||||
},
|
||||
_mouseover: function(button_id) {
|
||||
if( OSRM.G.active_alternative == button_id )
|
||||
@@ -141,31 +122,6 @@ _mouseout: function(button_id) {
|
||||
return;
|
||||
|
||||
OSRM.G.route.hideAlternativeRoute();
|
||||
},
|
||||
|
||||
// draw alternative GUI
|
||||
_drawGUI: function() {
|
||||
var buttons = OSRM.RoutingAlternatives._buttons;
|
||||
// draw clickable buttons
|
||||
for(var i=0, size=OSRM.G.alternative_count; i<size; i++) {
|
||||
var distance = OSRM.Utils.toHumanDistance(OSRM.G.response.alternative_summaries[i].total_distance);
|
||||
var time = OSRM.Utils.toHumanTime(OSRM.G.response.alternative_summaries[i].total_time);
|
||||
var tooltip = OSRM.loc("DISTANCE")+":"+distance+" "+OSRM.loc("DURATION")+":"+time;
|
||||
var buttonClass = (i == OSRM.G.active_alternative) ? "button-pressed" : "button";
|
||||
var data = '<a class="'+buttonClass+' top-right-button" id="'+buttons[i].id+'" title="'+tooltip+'">'+buttons[i].label+'</a>';
|
||||
document.getElementById('information-box-header').innerHTML = data + document.getElementById('information-box-header').innerHTML;
|
||||
}
|
||||
// draw non-clickable buttons
|
||||
for(var i=OSRM.G.alternative_count, size=buttons.length; i<size; ++i) {
|
||||
var data = '<a class="button-inactive top-right-button" id="'+buttons[i].id+'">'+buttons[i].label+'</a>';
|
||||
document.getElementById('information-box-header').innerHTML = data + document.getElementById('information-box-header').innerHTML;
|
||||
}
|
||||
// add events
|
||||
for(var i=0, size=OSRM.G.alternative_count; i<size; i++) {
|
||||
document.getElementById(buttons[i].id).onclick = function (button_id) { return function() {OSRM.RoutingAlternatives._click(button_id); }; }(i) ;
|
||||
document.getElementById(buttons[i].id).onmouseover = function (button_id) { return function() {OSRM.RoutingAlternatives._mouseover(button_id); }; } (i);
|
||||
document.getElementById(buttons[i].id).onmouseout = function (button_id) { return function() {OSRM.RoutingAlternatives._mouseout(button_id); }; } (i);
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
@@ -101,11 +101,14 @@ show: function(response) {
|
||||
body += '</table>';
|
||||
|
||||
// build header
|
||||
header = OSRM.RoutingDescription._buildHeader(OSRM.Utils.toHumanDistance(response.route_summary.total_distance), OSRM.Utils.toHumanTime(response.route_summary.total_time), route_link, gpx_link);
|
||||
header = OSRM.RoutingDescription._buildHeader(OSRM.Utils.toHumanDistance(response.route_summary.total_distance), OSRM.Utils.toHumanTime(response.route_summary.total_time), route_link, gpx_link);
|
||||
|
||||
// update DOM
|
||||
document.getElementById('information-box-header').innerHTML = header;
|
||||
document.getElementById('information-box').innerHTML = body;
|
||||
|
||||
// add alternative GUI (has to be done last since DOM has to be updated before events are registered)
|
||||
OSRM.RoutingAlternatives.show();
|
||||
},
|
||||
|
||||
// simple description
|
||||
|
||||
Reference in New Issue
Block a user