moved routing functions to OSRM namespace,

added functions to measure JSONP timings
This commit is contained in:
DennisSchiefer 2012-03-28 12:57:42 +01:00
parent 1a9776cff9
commit e132ac5c53
7 changed files with 107 additions and 100 deletions

View File

@ -60,7 +60,7 @@ _onclickResult: function(marker_id, lat, lon) {
OSRM.G.markers.route[index].show();
OSRM.G.markers.route[index].centerView();
getRoute(OSRM.C.FULL_DESCRIPTION);
OSRM.Routing.getRoute(OSRM.C.FULL_DESCRIPTION);
},

View File

@ -26,6 +26,7 @@ OSRM.JSONP = {
callbacks: {},
timeouts: {},
timers: {},
durations: {},
// default callback routines
@ -50,7 +51,7 @@ OSRM.JSONP = {
OSRM.JSONP.fences[id] = undefined; // clean fence
}
// OSRM.debug.log("[jsonp] timout handling: "+id);
OSRM.debug.log("[jsonp] timout handling: "+id+" "+ (new Date() - OSRM.JSONP.durations[id]) );
};
// wrap callback function
@ -66,7 +67,7 @@ OSRM.JSONP = {
OSRM.JSONP.fences[id] = undefined; // clean fence
}
// OSRM.debug.log("[jsonp] response handling: "+id);
OSRM.debug.log("[jsonp] response handling: "+id+" "+ (new Date() - OSRM.JSONP.durations[id]) );
};
// clean DOM (unfortunately, script elements cannot be reused by all browsers)
@ -83,6 +84,7 @@ OSRM.JSONP = {
// start timeout timer
OSRM.JSONP.timers[id] = setTimeout(OSRM.JSONP.timeouts[id], timeout);
OSRM.JSONP.durations[id] = new Date();
// OSRM.debug.log("[jsonp] init: "+id);
return true;
@ -94,5 +96,6 @@ OSRM.JSONP = {
OSRM.JSONP.callbacks = {};
OSRM.JSONP.timeouts = {};
OSRM.JSONP.timers = {};
OSRM.JSONP.durations = {};
}
};

View File

@ -114,12 +114,12 @@ onClick: function(e) {
}
}
getRoute(OSRM.C.FULL_DESCRIPTION);
OSRM.Routing.getRoute(OSRM.C.FULL_DESCRIPTION);
OSRM.G.markers.highlight.hide();
},
onDrag: function(e) {
this.parent.setPosition( e.target.getLatLng() );
getRoute(OSRM.C.NO_DESCRIPTION);
OSRM.Routing.getRoute(OSRM.C.NO_DESCRIPTION);
OSRM.Geocoder.updateLocation( this.parent.label );
},
onDragStart: function(e) {
@ -139,7 +139,7 @@ onDragStart: function(e) {
onDragEnd: function(e) {
OSRM.G.dragging = false;
this.parent.setPosition( e.target.getLatLng() );
getRoute(OSRM.C.FULL_DESCRIPTION);
OSRM.Routing.getRoute(OSRM.C.FULL_DESCRIPTION);
if (OSRM.G.route.isShown()) {
OSRM.G.route.hideOldRoute();
OSRM.G.route.hideUnnamedRoute();

View File

@ -63,7 +63,7 @@ findViaPosition: function( new_via_position ) {
var index = OSRM.G.markers.setVia(new_via_index, new_via_position);
OSRM.G.markers.route[index].show();
getRoute(OSRM.C.FULL_DESCRIPTION);
OSRM.Routing.getRoute(OSRM.C.FULL_DESCRIPTION);
return new_via_index;
}

View File

@ -44,7 +44,7 @@ or see http://www.gnu.org/licenses/agpl.txt.
<script src="OSRM.base.js" type="text/javascript"></script>
<script src="OSRM.config.js" type="text/javascript"></script>
<!-- <script defer="defer" src="OSRM.debug.js" type="text/javascript"></script> -->
<script defer="defer" src="OSRM.debug.js" type="text/javascript"></script>
<script src="OSRM.Browser.js" type="text/javascript"></script>
<script src="OSRM.GUI.js" type="text/javascript"></script>
@ -88,14 +88,14 @@ or see http://www.gnu.org/licenses/agpl.txt.
<table class="full">
<tr>
<td id="gui-search-source-label">Start:</td>
<td><input id="input-source-name" class="input-box" type="text" maxlength="200" value="" title="Startposition eingeben" onchange="inputChanged(OSRM.C.SOURCE_LABEL);" /></td>
<td class="right"><a class="button not-selectable" id="gui-search-source" onclick="showMarker('source')">Zeigen</a></td>
<td><input id="input-source-name" class="input-box" type="text" maxlength="200" value="" title="Startposition eingeben" onchange="OSRM.Routing.inputChanged(OSRM.C.SOURCE_LABEL);" /></td>
<td class="right"><a class="button not-selectable" id="gui-search-source" onclick="OSRM.Routing.showMarker('source')">Zeigen</a></td>
</tr>
<tr>
<td id="gui-search-target-label">Ende:</td>
<td><input id="input-target-name" class="input-box" type="text" maxlength="200" value="" title="Zielposition eingeben" onchange="inputChanged(OSRM.C.TARGET_LABEL);" /></td>
<td class="right"><a class="button not-selectable" id="gui-search-target" onclick="showMarker('target');">Zeigen</a></td>
<td><input id="input-target-name" class="input-box" type="text" maxlength="200" value="" title="Zielposition eingeben" onchange="OSRM.Routing.inputChanged(OSRM.C.TARGET_LABEL);" /></td>
<td class="right"><a class="button not-selectable" id="gui-search-target" onclick="OSRM.Routing.showMarker('target');">Zeigen</a></td>
</tr>
</table>
@ -103,8 +103,8 @@ or see http://www.gnu.org/licenses/agpl.txt.
<div class="vquad"></div>
<table style="width:100%">
<tr>
<td> <a class="button not-selectable" id="gui-reset" onclick="resetRouting();">Reset</a></td>
<td class="right"> <a class="button not-selectable" id="gui-reverse" onclick="reverseRouting();">Umdrehen</a></td>
<td> <a class="button not-selectable" id="gui-reset" onclick="OSRM.Routing.resetRouting();">Reset</a></td>
<td class="right"> <a class="button not-selectable" id="gui-reverse" onclick="OSRM.Routing.reverseRouting();">Umdrehen</a></td>
</tr>
</table>
@ -112,7 +112,7 @@ or see http://www.gnu.org/licenses/agpl.txt.
<div class="vquad"></div>
<div class="main-options not-selectable" id="options-toggle" onclick="OSRM.GUI.toggleOptions()">Options</div>
<div class="main-options not-selectable" id="options-box">
<input type="checkbox" id="option-highlight-nonames" name="main-options" value="highlight-nonames" onclick="getRoute(OSRM.C.FULL_DESCRIPTION)" /><span id="gui-option-highlight-nonames-label">Unbenannte Straßen hervorheben</span>
<input type="checkbox" id="option-highlight-nonames" name="main-options" value="highlight-nonames" onclick="OSRM.Routing.getRoute(OSRM.C.FULL_DESCRIPTION)" /><span id="gui-option-highlight-nonames-label">Unbenannte Straßen hervorheben</span>
</div>
</div>

View File

@ -30,7 +30,7 @@ function init() {
initLocale();
initGUI();
initMap();
initRouting();
OSRM.Routing.init();
// check if the URL contains some GET parameter, e.g. for the route
checkURL();
@ -168,7 +168,7 @@ function initMap() {
OSRM.G.map.setView( position, OSRM.DEFAULTS.ZOOM_LEVEL);
// map events
OSRM.G.map.on('zoomend', function(e) { getRoute(OSRM.C.FULL_DESCRIPTION); });
OSRM.G.map.on('zoomend', function(e) { OSRM.Routing.getRoute(OSRM.C.FULL_DESCRIPTION); });
OSRM.G.map.on('contextmenu', function(e) {});
OSRM.G.map.on('click', function(e) {
if( !OSRM.G.markers.hasSource() ) {
@ -176,13 +176,13 @@ function initMap() {
OSRM.Geocoder.updateAddress( OSRM.C.SOURCE_LABEL, OSRM.C.DO_FALLBACK_TO_LAT_LNG );
OSRM.G.markers.route[index].show();
OSRM.G.markers.route[index].centerView( OSRM.G.map.getZoom() );
getRoute( OSRM.C.FULL_DESCRIPTION );
OSRM.Routing.getRoute( OSRM.C.FULL_DESCRIPTION );
} else if( !OSRM.G.markers.hasTarget() ) {
var index = OSRM.G.markers.setTarget( e.latlng );
OSRM.Geocoder.updateAddress( OSRM.C.TARGET_LABEL, OSRM.C.DO_FALLBACK_TO_LAT_LNG );
OSRM.G.markers.route[index].show();
OSRM.G.markers.route[index].centerView( OSRM.G.map.getZoom() );
getRoute( OSRM.C.FULL_DESCRIPTION );
OSRM.Routing.getRoute( OSRM.C.FULL_DESCRIPTION );
}
} );
}
@ -275,6 +275,6 @@ function checkURL(){
}
// compute route
getRoute(OSRM.C.FULL_DESCRIPTION);
OSRM.Routing.getRoute(OSRM.C.FULL_DESCRIPTION);
}
}

View File

@ -32,28 +32,30 @@ OSRM.GLOBALS.pending = false;
OSRM.GLOBALS.pendingTimer = null;
OSRM.Routing = {
// init routing data structures
function initRouting() {
init: function() {
OSRM.G.route = new OSRM.Route();
OSRM.G.markers = new OSRM.Markers();
}
},
// -- JSONP processing --
// process JSONP response of routing server
function timeoutRouteSimple() {
showNoRouteGeometry();
showNoRouteDescription();
timeoutRouteSimple: function() {
OSRM.Routing.showNoRouteGeometry();
OSRM.Routing.showNoRouteDescription();
document.getElementById('information-box').innerHTML = "<br><p style='font-size:14px;font-weight:bold;text-align:center;'>"+OSRM.loc("TIMED_OUT")+".<p>";
}
function timeoutRoute() {
showNoRouteGeometry();
},
timeoutRoute: function() {
OSRM.Routing.showNoRouteGeometry();
OSRM.G.route.hideUnnamedRoute();
showNoRouteDescription();
OSRM.Routing.showNoRouteDescription();
document.getElementById('information-box').innerHTML = "<br><p style='font-size:14px;font-weight:bold;text-align:center;'>"+OSRM.loc("TIMED_OUT")+".<p>";
}
function showRouteSimple(response) {
},
showRouteSimple: function(response) {
if(!response)
return;
@ -61,88 +63,88 @@ function showRouteSimple(response) {
return;
if( response.status == 207) {
showNoRouteGeometry();
showNoRouteDescription();
OSRM.Routing.showNoRouteGeometry();
OSRM.Routing.showNoRouteDescription();
document.getElementById('information-box').innerHTML = "<br><p style='font-size:14px;font-weight:bold;text-align:center;'>"+OSRM.loc("YOUR_ROUTE_IS_BEING_COMPUTED")+".<p>";
} else {
showRouteGeometry(response);
showRouteDescriptionSimple(response);
OSRM.Routing.showRouteGeometry(response);
OSRM.Routing.showRouteDescriptionSimple(response);
}
updateHints(response);
OSRM.Routing.updateHints(response);
// // TODO: hack to process final drag event, if it was fenced, but we are still dragging (alternative approach)
// if(OSRM.G.pending) {
// clearTimeout(OSRM.G.pendingTimer);
// OSRM.G.pendingTimer = setTimeout(timeoutDrag,100);
// OSRM.G.pendingTimer = setTimeout(OSRM.Routing.timeoutDrag,100);
// }
}
function showRoute(response) {
},
showRoute: function(response) {
if(!response)
return;
if(response.status == 207) {
showNoRouteGeometry();
OSRM.Routing.showNoRouteGeometry();
OSRM.G.route.hideUnnamedRoute();
showNoRouteDescription();
OSRM.Routing.showNoRouteDescription();
document.getElementById('information-box').innerHTML = "<br><p style='font-size:14px;font-weight:bold;text-align:center;'>"+OSRM.loc("NO_ROUTE_FOUND")+".<p>";
} else {
showRouteGeometry(response);
showRouteNonames(response);
showRouteDescription(response);
snapRoute();
OSRM.Routing.showRouteGeometry(response);
OSRM.Routing.showRouteNonames(response);
OSRM.Routing.showRouteDescription(response);
OSRM.Routing.snapRoute();
}
updateHints(response);
}
OSRM.Routing.updateHints(response);
},
// show route geometry
function showNoRouteGeometry() {
showNoRouteGeometry: function() {
var positions = [];
for(var i=0; i<OSRM.G.markers.route.length;i++)
positions.push( OSRM.G.markers.route[i].getPosition() );
OSRM.G.route.showRoute(positions, OSRM.Route.NOROUTE);
}
function showRouteGeometry(response) {
},
showRouteGeometry: function(response) {
OSRM.G.via_points = response.via_points.slice(0);
var geometry = decodeRouteGeometry(response.route_geometry, 5);
var geometry = OSRM.Routing.decodeRouteGeometry(response.route_geometry, 5);
var points = [];
for( var i=0; i < geometry.length; i++) {
points.push( new L.LatLng(geometry[i][0], geometry[i][1]) );
}
OSRM.G.route.showRoute(points, OSRM.Route.ROUTE);
}
},
// route description display (and helper functions)
function onClickRouteDescription(geometry_index) {
onClickRouteDescription: function(geometry_index) {
var positions = OSRM.G.route.getPositions();
OSRM.G.markers.highlight.setPosition( positions[geometry_index] );
OSRM.G.markers.highlight.show();
OSRM.G.markers.highlight.centerView(OSRM.DEFAULTS.HIGHLIGHT_ZOOM_LEVEL);
}
function onClickCreateShortcut(src){
},
onClickCreateShortcut: function(src){
src += '&z='+ OSRM.G.map.getZoom() + '&center=' + OSRM.G.map.getCenter().lat + ',' + OSRM.G.map.getCenter().lng;
OSRM.JSONP.call(OSRM.DEFAULTS.HOST_SHORTENER_URL+src, showRouteLink, showRouteLink_TimeOut, 2000, 'shortener');
OSRM.JSONP.call(OSRM.DEFAULTS.HOST_SHORTENER_URL+src, OSRM.Routing.showRouteLink, OSRM.Routing.showRouteLink_TimeOut, 2000, 'shortener');
document.getElementById('route-prelink').innerHTML = '['+OSRM.loc("GENERATE_LINK_TO_ROUTE")+']';
}
function showRouteLink(response){
},
showRouteLink: function(response){
document.getElementById('route-prelink').innerHTML = '[<a id="gpx-link" class = "text-selectable" href="' +response.ShortURL+ '">'+response.ShortURL+'</a>]';
}
function showRouteLink_TimeOut(){
},
showRouteLink_TimeOut: function(){
document.getElementById('route-prelink').innerHTML = '['+OSRM.loc("LINK_TO_ROUTE_TIMEOUT")+']';
}
function showRouteDescription(response) {
},
showRouteDescription: function(response) {
// compute query string
var query_string = '?rebuild=1';
for(var i=0; i<OSRM.G.markers.route.length; i++)
query_string += '&loc=' + OSRM.G.markers.route[i].getLat() + ',' + OSRM.G.markers.route[i].getLng();
// create link to the route
var route_link ='<span class="route-summary" id="route-prelink">[<a id="gpx-link" onclick="onClickCreateShortcut(\'' + OSRM.DEFAULTS.WEBSITE_URL + query_string + '\')">'+OSRM.loc("GET_LINK_TO_ROUTE")+'</a>]</span>';
var route_link ='<span class="route-summary" id="route-prelink">[<a id="gpx-link" onclick="OSRM.Routing.onClickCreateShortcut(\'' + OSRM.DEFAULTS.WEBSITE_URL + query_string + '\')">'+OSRM.loc("GET_LINK_TO_ROUTE")+'</a>]</span>';
// create GPX link
var gpx_link = '<span class="route-summary">[<a id="gpx-link" onClick="document.location.href=\'' + OSRM.DEFAULTS.HOST_ROUTING_URL + query_string + '&output=gpx\';">'+OSRM.loc("GPX_FILE")+'</a>]</span>';
@ -159,11 +161,11 @@ function showRouteDescription(response) {
route_desc += '<tr class="'+rowstyle+'">';
route_desc += '<td class="result-directions">';
route_desc += '<img width="18px" src="images/'+getDirectionIcon(response.route_instructions[i][0])+'" alt="" />';
route_desc += '<img width="18px" src="images/'+OSRM.Routing.getDirectionIcon(response.route_instructions[i][0])+'" alt="" />';
route_desc += "</td>";
route_desc += '<td class="result-items">';
route_desc += '<span class="result-item" onclick="onClickRouteDescription('+response.route_instructions[i][3]+')">';
route_desc += '<span class="result-item" onclick="OSRM.Routing.onClickRouteDescription('+response.route_instructions[i][3]+')">';
route_desc += response.route_instructions[i][0];
if( i == 0 )
route_desc += ' ' + OSRM.loc( response.route_instructions[i][6] );
@ -200,8 +202,8 @@ function showRouteDescription(response) {
document.getElementById('information-box-headline').innerHTML = headline;
document.getElementById('information-box').innerHTML = output;
}
function showRouteDescriptionSimple(response) {
},
showRouteDescriptionSimple: function(response) {
headline = OSRM.loc("ROUTE_DESCRIPTION")+":<br>";
headline += "<span class='route-summary'>"
+ OSRM.loc("DISTANCE")+": " + OSRM.Utils.metersToDistance(response.route_summary.total_distance)
@ -212,8 +214,8 @@ function showRouteDescriptionSimple(response) {
document.getElementById('information-box-headline').innerHTML = headline;
document.getElementById('information-box').innerHTML = "<br><p style='font-size:14px;font-weight:bold;text-align:center;'>"+OSRM.loc("YOUR_ROUTE_IS_BEING_COMPUTED")+".<p>";
}
function showNoRouteDescription() {
},
showNoRouteDescription: function() {
headline = OSRM.loc("ROUTE_DESCRIPTION")+":<br>";
headline += "<span class='route-summary'>"
+ OSRM.loc("DISTANCE")+": N/A"
@ -224,11 +226,11 @@ function showNoRouteDescription() {
document.getElementById('information-box-headline').innerHTML = headline;
document.getElementById('information-box').innerHTML = "<br><p style='font-size:14px;font-weight:bold;text-align:center;'>"+OSRM.loc("YOUR_ROUTE_IS_BEING_COMPUTED")+".<p>";
}
},
// unnamed streets display
function showRouteNonames(response) {
showRouteNonames: function(response) {
// do not display unnamed streets?
if( document.getElementById('option-highlight-nonames').checked == false) {
OSRM.G.route.hideUnnamedRoute();
@ -245,7 +247,7 @@ function showRouteNonames(response) {
}
// aggregate geometry for unnamed streets
var geometry = decodeRouteGeometry(response.route_geometry, 5);
var geometry = OSRM.Routing.decodeRouteGeometry(response.route_geometry, 5);
var is_named = true;
var current_positions = [];
var all_positions = [];
@ -266,13 +268,13 @@ function showRouteNonames(response) {
// display unnamed streets
OSRM.G.route.showUnnamedRoute(all_positions);
}
},
//-- main function --
// generate server calls to query routes
function getRoute(do_description) {
getRoute: function(do_description) {
// if source or target are not set -> hide route
if( OSRM.G.markers.route.length < 2 ) {
@ -297,13 +299,13 @@ function getRoute(do_description) {
// decide whether it is a dragging call or a normal one
if (do_description) {
callback = showRoute;
timeout = timeoutRoute;
callback = OSRM.Routing.showRoute;
timeout = OSRM.Routing.timeoutRoute;
source +='&instructions=true';
type = 'route';
} else {
callback = showRouteSimple;
timeout = timeoutRouteSimple;
callback = OSRM.Routing.showRouteSimple;
timeout = OSRM.Routing.timeoutRouteSimple;
source +='&instructions=false';
type = 'dragging';
}
@ -314,7 +316,7 @@ function getRoute(do_description) {
// TODO: hack to process final drag event, if it was fenced, but we are still dragging
if(called == false && !do_description) {
clearTimeout(OSRM.G.pendingTimer);
OSRM.G.pendingTimer = setTimeout(timeoutDrag,OSRM.DEFAULTS.JSONP_TIMEOUT);
OSRM.G.pendingTimer = setTimeout(OSRM.Routing.timeoutDrag,OSRM.DEFAULTS.JSONP_TIMEOUT);
}
else {
clearTimeout(OSRM.G.pendingTimer);
@ -326,17 +328,17 @@ function getRoute(do_description) {
// clearTimeout(OSRM.G.pendingTimer);
// OSRM.G.pending = false;
// }
}
function timeoutDrag() {
},
timeoutDrag: function() {
OSRM.G.markers.route[OSRM.G.dragid].hint = null;
getRoute(OSRM.C.NO_DESCRIPTION);
}
OSRM.Routing.getRoute(OSRM.C.NO_DESCRIPTION);
},
//-- helper functions --
//decode compressed route geometry
function decodeRouteGeometry(encoded, precision) {
decodeRouteGeometry: function(encoded, precision) {
precision = Math.pow(10, -precision);
var len = encoded.length, index=0, lat=0, lng = 0, array = [];
while (index < len) {
@ -360,18 +362,18 @@ function decodeRouteGeometry(encoded, precision) {
array.push([lat * precision, lng * precision]);
}
return array;
}
},
// update hints of all markers
function updateHints(response) {
updateHints: function(response) {
var hint_locations = response.hint_data.locations;
OSRM.G.markers.checksum = response.hint_data.checksum;
for(var i=0; i<hint_locations.length; i++)
OSRM.G.markers.route[i].hint = hint_locations[i];
}
},
// snap all markers to the received route
function snapRoute() {
snapRoute: function() {
var positions = OSRM.G.route.getPositions();
OSRM.G.markers.route[0].setPosition( positions[0] );
@ -381,11 +383,11 @@ function snapRoute() {
OSRM.Geocoder.updateAddress(OSRM.C.SOURCE_LABEL);
OSRM.Geocoder.updateAddress(OSRM.C.TARGET_LABEL);
}
},
// map driving instructions to icons
// [TODO: better implementation, language-safe]
function getDirectionIcon(name) {
getDirectionIcon: function(name) {
var directions = {
"Turn left":"turn-left.png",
"Turn right":"turn-right.png",
@ -414,13 +416,13 @@ function getDirectionIcon(name) {
return directions[name];
else
return "default.png";
}
},
// -- gui functions --
// click: button "reset"
function resetRouting() {
resetRouting: function() {
document.getElementById('input-source-name').value = "";
document.getElementById('input-target-name').value = "";
@ -432,10 +434,10 @@ function resetRouting() {
document.getElementById('information-box-headline').innerHTML = "";
OSRM.JSONP.reset();
}
},
// click: button "reverse"
function reverseRouting() {
reverseRouting: function() {
// invert input boxes
var tmp = document.getElementById("input-source-name").value;
document.getElementById("input-source-name").value = document.getElementById("input-target-name").value;
@ -461,16 +463,16 @@ function reverseRouting() {
// recompute route
if( OSRM.G.route.isShown() ) {
getRoute(OSRM.C.FULL_DESCRIPTION);
OSRM.Routing.getRoute(OSRM.C.FULL_DESCRIPTION);
OSRM.G.markers.highlight.hide();
} else {
document.getElementById('information-box').innerHTML = "";
document.getElementById('information-box-headline').innerHTML = "";
}
}
},
// click: button "show"
function showMarker(marker_id) {
showMarker: function(marker_id) {
if( OSRM.JSONP.fences["geocoder_source"] || OSRM.JSONP.fences["geocoder_target"] )
return;
@ -478,13 +480,15 @@ function showMarker(marker_id) {
OSRM.G.markers.route[0].centerView();
else if( marker_id == OSRM.C.TARGET_LABEL && OSRM.G.markers.hasTarget() )
OSRM.G.markers.route[OSRM.G.markers.route.length-1].centerView();
}
},
// changed: any inputbox (is called when return is pressed [after] or focus is lost [before])
function inputChanged(marker_id) {
inputChanged: function(marker_id) {
if( marker_id == OSRM.C.SOURCE_LABEL)
OSRM.Geocoder.call(OSRM.C.SOURCE_LABEL, document.getElementById('input-source-name').value);
else if( marker_id == OSRM.C.TARGET_LABEL)
OSRM.Geocoder.call(OSRM.C.TARGET_LABEL, document.getElementById('input-target-name').value);
}
};