added stuff to alternatives
This commit is contained in:
		
							parent
							
								
									80d2918203
								
							
						
					
					
						commit
						537b9b0623
					
				| @ -19,7 +19,7 @@ or see http://www.gnu.org/licenses/agpl.txt. | |||||||
| // [has to be loaded directly after OSRM.base]
 | // [has to be loaded directly after OSRM.base]
 | ||||||
| 
 | 
 | ||||||
| OSRM.DEFAULTS = { | OSRM.DEFAULTS = { | ||||||
| 	HOST_ROUTING_URL: 'http://i10pc68.ira.uka.de:5000/viaroute', | 	HOST_ROUTING_URL: 'http://router.project-osrm.org/viaroute', | ||||||
| 	HOST_SHORTENER_URL: 'http://map.project-osrm.org/shorten/', | 	HOST_SHORTENER_URL: 'http://map.project-osrm.org/shorten/', | ||||||
| 	HOST_TIMESTAMP_URL: 'http://router.project-osrm.org/timestamp', | 	HOST_TIMESTAMP_URL: 'http://router.project-osrm.org/timestamp', | ||||||
| 	HOST_GEOCODER_URL: 'http://nominatim.openstreetmap.org/search', | 	HOST_GEOCODER_URL: 'http://nominatim.openstreetmap.org/search', | ||||||
|  | |||||||
| @ -216,7 +216,13 @@ OSRM.parseParameters = function(){ | |||||||
| 			if(coordinates.length!=2 || !OSRM.Utils.isLatitude(coordinates[0]) || !OSRM.Utils.isLongitude(coordinates[1]) ) | 			if(coordinates.length!=2 || !OSRM.Utils.isLatitude(coordinates[0]) || !OSRM.Utils.isLongitude(coordinates[1]) ) | ||||||
| 				return;				 | 				return;				 | ||||||
| 			params.center = new L.LatLng( coordinates[0], coordinates[1]);			 | 			params.center = new L.LatLng( coordinates[0], coordinates[1]);			 | ||||||
| 		}		 | 		} | ||||||
|  | 		else if(name_val[0] == 'alt') { | ||||||
|  | 			var active_alternative = Number(name_val[1]); | ||||||
|  | 			if( active_alternative<0 || active_alternative>OSRM.RoutingAlternatives>10)	// using 10 as arbitrary upper limit
 | ||||||
|  | 				return; | ||||||
|  | 			params.active_alternative = active_alternative; | ||||||
|  | 		} | ||||||
| 	} | 	} | ||||||
| 		 | 		 | ||||||
| 	// case 1: destination given
 | 	// case 1: destination given
 | ||||||
| @ -255,6 +261,9 @@ OSRM.parseParameters = function(){ | |||||||
| 		} else { | 		} else { | ||||||
| 			OSRM.G.map.setView(params.center, params.zoom); | 			OSRM.G.map.setView(params.center, params.zoom); | ||||||
| 		} | 		} | ||||||
|  | 		 | ||||||
|  | 		// set active alternative (if via points are set or alternative does not exists: automatic fallback to shortest route)
 | ||||||
|  | 		OSRM.G.active_alternative = params.active_alternative; | ||||||
| 			 | 			 | ||||||
| 		// compute route
 | 		// compute route
 | ||||||
| 		OSRM.Routing.getRoute(); | 		OSRM.Routing.getRoute(); | ||||||
|  | |||||||
| @ -26,9 +26,7 @@ OSRM.RoutingAlternatives = { | |||||||
| // data of gui buttons for alternativess
 | // data of gui buttons for alternativess
 | ||||||
| _buttons: [  | _buttons: [  | ||||||
| 	{id:"gui-a", label:"A"}, | 	{id:"gui-a", label:"A"}, | ||||||
| 	{id:"gui-b", label:"B"}, | 	{id:"gui-b", label:"B"} | ||||||
| 	{id:"gui-c", label:"C"}, |  | ||||||
| 	{id:"gui-d", label:"D"} |  | ||||||
| ], | ], | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| @ -36,21 +34,20 @@ _buttons: [ | |||||||
| prepare: function(response) { | prepare: function(response) { | ||||||
| 	// store basic information
 | 	// store basic information
 | ||||||
| 	OSRM.G.alternative_count = response.alternative_geometries.length + 1; | 	OSRM.G.alternative_count = response.alternative_geometries.length + 1; | ||||||
| 	OSRM.G.alternative_active = 0; | 	OSRM.G.active_alternative = 0; | ||||||
| 	 | 	 | ||||||
| 	// do nothing if there is no alternative
 | 	// do nothing if there is no alternative
 | ||||||
| 	if( OSRM.G.alternative_count == 1 ) | //	if( OSRM.G.alternative_count == 1 )
 | ||||||
| 		return; | //		return;
 | ||||||
| 
 | 
 | ||||||
| 	// move best route to alternative array
 | 	// move best route to alternative array
 | ||||||
| 	console.log("prepare"); |  | ||||||
| 	var the_response = OSRM.G.response; | 	var the_response = OSRM.G.response; | ||||||
| 	the_response.alternative_geometries.unshift( response.route_geometry ); | 	the_response.alternative_geometries.unshift( response.route_geometry ); | ||||||
| 	the_response.alternative_instructions.unshift( response.route_instructions ); | 	the_response.alternative_instructions.unshift( response.route_instructions ); | ||||||
| 	the_response.alternative_summaries.unshift( response.route_summary ); | 	the_response.alternative_summaries.unshift( response.route_summary ); | ||||||
| 	 | 	 | ||||||
| 	// add alternative GUI
 | 	// add alternative GUI
 | ||||||
| 	OSRM.RoutingAlternatives._addGUI(); | 	OSRM.RoutingAlternatives._drawGUI(); | ||||||
| }, | }, | ||||||
| prepare_Redraw: function(response) { | prepare_Redraw: function(response) { | ||||||
| 	// do nothing if there is no alternative
 | 	// do nothing if there is no alternative
 | ||||||
| @ -68,29 +65,29 @@ prepare_Redraw: function(response) { | |||||||
| 	the_response.alternative_summaries.unshift( response.route_summary );	 | 	the_response.alternative_summaries.unshift( response.route_summary );	 | ||||||
| 
 | 
 | ||||||
| 	// switch data
 | 	// switch data
 | ||||||
| 	the_response.route_geometry = the_response.alternative_geometries[OSRM.G.alternative_active]; | 	the_response.route_geometry = the_response.alternative_geometries[OSRM.G.active_alternative]; | ||||||
| 	the_response.route_instructions = the_response.alternative_instructions[OSRM.G.alternative_active]; | 	the_response.route_instructions = the_response.alternative_instructions[OSRM.G.active_alternative]; | ||||||
| 	the_response.route_summary = the_response.alternative_summaries[OSRM.G.alternative_active]; | 	the_response.route_summary = the_response.alternative_summaries[OSRM.G.active_alternative]; | ||||||
| }, | }, | ||||||
| 
 | 
 | ||||||
| // press one of the buttons
 | // switch active alternative and redraw buttons accordingly
 | ||||||
| press: function(button_id) { | setActive: function(button_id) { | ||||||
| 	// switch active alternative
 | 	// switch active alternative
 | ||||||
| 	OSRM.G.alternative_active = button_id; | 	OSRM.G.active_alternative = button_id; | ||||||
| 	 | 	 | ||||||
| 	// redraw buttons
 | 	// redraw clickable buttons
 | ||||||
| 	var buttons = OSRM.RoutingAlternatives._buttons; | 	var buttons = OSRM.RoutingAlternatives._buttons; | ||||||
| 	for(var i=0, size=OSRM.G.alternative_count; i<size; i++) { | 	for(var i=0, size=OSRM.G.alternative_count; i<size; i++) { | ||||||
| 		document.getElementById( buttons[i].id ).className = (button_id == i) ? "button-pressed top-right-button" : "button top-right-button"; | 		document.getElementById( buttons[i].id ).className = (button_id == i) ? "button-pressed top-right-button" : "button top-right-button"; | ||||||
| 	} | 	} | ||||||
|  | 	// do nothing for non-clickable buttons
 | ||||||
| }, | }, | ||||||
| 
 | 
 | ||||||
| // mouse events on buttons
 | // mouse events on buttons
 | ||||||
| _click: function(button_id) { | _click: function(button_id) { | ||||||
| 	console.log("click "+button_id);	 | 	if( OSRM.G.active_alternative == button_id ) | ||||||
| 	if( OSRM.G.alternative_active == button_id ) |  | ||||||
| 		return; | 		return; | ||||||
| 	OSRM.RoutingAlternatives.press(button_id); | 	OSRM.RoutingAlternatives.setActive(button_id); | ||||||
| 	OSRM.G.route.hideAlternativeRoute(); | 	OSRM.G.route.hideAlternativeRoute(); | ||||||
| 	 | 	 | ||||||
| 	// switch data
 | 	// switch data
 | ||||||
| @ -103,40 +100,45 @@ _click: function(button_id) { | |||||||
| 	OSRM.RoutingGeometry.show(the_response); | 	OSRM.RoutingGeometry.show(the_response); | ||||||
| 	OSRM.RoutingNoNames.show(the_response); | 	OSRM.RoutingNoNames.show(the_response); | ||||||
| 	OSRM.RoutingDescription.show(the_response); | 	OSRM.RoutingDescription.show(the_response); | ||||||
| 	OSRM.RoutingAlternatives._addGUI(); | 	OSRM.RoutingAlternatives._drawGUI(); | ||||||
| }, | }, | ||||||
| _mouseover: function(button_id) { | _mouseover: function(button_id) { | ||||||
| 	console.log("over "+button_id); | 	if( OSRM.G.active_alternative == button_id ) | ||||||
| 	if( OSRM.G.alternative_active == button_id ) |  | ||||||
| 		return; | 		return; | ||||||
| 
 | 
 | ||||||
| 	var geometry = OSRM.RoutingGeometry._decode( OSRM.G.response.alternative_geometries[button_id], 5); | 	var geometry = OSRM.RoutingGeometry._decode( OSRM.G.response.alternative_geometries[button_id], 5); | ||||||
| 	OSRM.G.route.showAlternativeRoute(geometry); | 	OSRM.G.route.showAlternativeRoute(geometry); | ||||||
| }, | }, | ||||||
| _mouseout: function(button_id) { | _mouseout: function(button_id) { | ||||||
| 	console.log("out "+button_id); | 	if( OSRM.G.active_alternative == button_id ) | ||||||
| 	if( OSRM.G.alternative_active == button_id ) |  | ||||||
| 		return; | 		return; | ||||||
| 	 | 	 | ||||||
| 	OSRM.G.route.hideAlternativeRoute();		 | 	OSRM.G.route.hideAlternativeRoute();		 | ||||||
| }, | }, | ||||||
| 
 | 
 | ||||||
| // add alternatives
 | // draw alternative GUI
 | ||||||
| _addGUI: function() { | _drawGUI: function() { | ||||||
| 	var buttons = OSRM.RoutingAlternatives._buttons; | 	var buttons = OSRM.RoutingAlternatives._buttons; | ||||||
|  | 	// draw clickable buttons
 | ||||||
| 	for(var i=0, size=OSRM.G.alternative_count; i<size; i++) { | 	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 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 time = OSRM.Utils.toHumanTime(OSRM.G.response.alternative_summaries[i].total_time); | ||||||
| 		var tooltip = OSRM.loc("DISTANCE")+":"+distance+" "+OSRM.loc("DURATION")+":"+time; | 		var tooltip = OSRM.loc("DISTANCE")+":"+distance+" "+OSRM.loc("DURATION")+":"+time; | ||||||
| 		var data = '<a class="button top-right-button" id="'+buttons[i].id+'" title="'+tooltip+'">'+buttons[i].label+'</a>'; | 		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; | 		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++) { | 	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).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).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); | 		document.getElementById(buttons[i].id).onmouseout = function (button_id) { return function() {OSRM.RoutingAlternatives._mouseout(button_id); }; } (i); | ||||||
| 	} | 	} | ||||||
| 	OSRM.RoutingAlternatives.press( OSRM.G.alternative_active ); |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| }; | }; | ||||||
| @ -29,6 +29,7 @@ onClickRouteDescription: function(lat, lng) { | |||||||
| }, | }, | ||||||
| onClickCreateShortcut: function(src){ | onClickCreateShortcut: function(src){ | ||||||
| 	src += '&z='+ OSRM.G.map.getZoom() + '¢er=' + OSRM.G.map.getCenter().lat.toFixed(6) + ',' + OSRM.G.map.getCenter().lng.toFixed(6); | 	src += '&z='+ OSRM.G.map.getZoom() + '¢er=' + OSRM.G.map.getCenter().lat.toFixed(6) + ',' + OSRM.G.map.getCenter().lng.toFixed(6); | ||||||
|  | 	src += '&alt='+OSRM.G.active_alternative; | ||||||
| 	src += '&df=' + OSRM.G.DISTANCE_FORMAT; | 	src += '&df=' + OSRM.G.DISTANCE_FORMAT; | ||||||
| 	 | 	 | ||||||
| 	var source = OSRM.DEFAULTS.SHORTENER_PARAMETERS.replace(/%url/, OSRM.DEFAULTS.HOST_SHORTENER_URL+src);  | 	var source = OSRM.DEFAULTS.SHORTENER_PARAMETERS.replace(/%url/, OSRM.DEFAULTS.HOST_SHORTENER_URL+src);  | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user