From e132ac5c53c0f320debc29e266b957ccd67c7004 Mon Sep 17 00:00:00 2001 From: DennisSchiefer Date: Wed, 28 Mar 2012 12:57:42 +0100 Subject: [PATCH] moved routing functions to OSRM namespace, added functions to measure JSONP timings --- WebContent/OSRM.Geocoder.js | 2 +- WebContent/OSRM.JSONP.js | 7 +- WebContent/OSRM.Markers.js | 6 +- WebContent/OSRM.Via.js | 2 +- WebContent/main.html | 16 ++-- WebContent/main.js | 12 +-- WebContent/routing.js | 162 ++++++++++++++++++------------------ 7 files changed, 107 insertions(+), 100 deletions(-) diff --git a/WebContent/OSRM.Geocoder.js b/WebContent/OSRM.Geocoder.js index fef263524..bc1a6f14e 100644 --- a/WebContent/OSRM.Geocoder.js +++ b/WebContent/OSRM.Geocoder.js @@ -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); }, diff --git a/WebContent/OSRM.JSONP.js b/WebContent/OSRM.JSONP.js index 3a337bd57..430fc8beb 100644 --- a/WebContent/OSRM.JSONP.js +++ b/WebContent/OSRM.JSONP.js @@ -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 = {}; } }; \ No newline at end of file diff --git a/WebContent/OSRM.Markers.js b/WebContent/OSRM.Markers.js index 801b2359e..e8fb8256a 100644 --- a/WebContent/OSRM.Markers.js +++ b/WebContent/OSRM.Markers.js @@ -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(); diff --git a/WebContent/OSRM.Via.js b/WebContent/OSRM.Via.js index d072b9797..e8ab0ee32 100644 --- a/WebContent/OSRM.Via.js +++ b/WebContent/OSRM.Via.js @@ -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; } diff --git a/WebContent/main.html b/WebContent/main.html index c42a10374..696a25d5f 100644 --- a/WebContent/main.html +++ b/WebContent/main.html @@ -44,7 +44,7 @@ or see http://www.gnu.org/licenses/agpl.txt. - + @@ -88,14 +88,14 @@ or see http://www.gnu.org/licenses/agpl.txt. - - + + - - + +
Start:ZeigenZeigen
Ende:ZeigenZeigen
@@ -103,8 +103,8 @@ or see http://www.gnu.org/licenses/agpl.txt.
- - + +
Reset Umdrehen Reset Umdrehen
@@ -112,7 +112,7 @@ or see http://www.gnu.org/licenses/agpl.txt.
Options
- Unbenannte Straßen hervorheben + Unbenannte Straßen hervorheben
diff --git a/WebContent/main.js b/WebContent/main.js index dd97ca4eb..03987f151 100644 --- a/WebContent/main.js +++ b/WebContent/main.js @@ -30,8 +30,8 @@ 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); } } diff --git a/WebContent/routing.js b/WebContent/routing.js index 7a44fd013..1f330fea8 100644 --- a/WebContent/routing.js +++ b/WebContent/routing.js @@ -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 = "

"+OSRM.loc("TIMED_OUT")+".

"; -} -function timeoutRoute() { - showNoRouteGeometry(); +}, +timeoutRoute: function() { + OSRM.Routing.showNoRouteGeometry(); OSRM.G.route.hideUnnamedRoute(); - showNoRouteDescription(); + OSRM.Routing.showNoRouteDescription(); document.getElementById('information-box').innerHTML = "

"+OSRM.loc("TIMED_OUT")+".

"; -} -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 = "

"+OSRM.loc("YOUR_ROUTE_IS_BEING_COMPUTED")+".

"; } 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 = "

"+OSRM.loc("NO_ROUTE_FOUND")+".

"; } 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'+response.ShortURL+']'; -} -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.loc("GET_LINK_TO_ROUTE")+']'; + var route_link ='['+OSRM.loc("GET_LINK_TO_ROUTE")+']'; // create GPX link var gpx_link = '['+OSRM.loc("GPX_FILE")+']'; @@ -159,11 +161,11 @@ function showRouteDescription(response) { route_desc += ''; route_desc += ''; - route_desc += ''; + route_desc += ''; route_desc += ""; route_desc += ''; - route_desc += ''; + route_desc += ''; 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")+":
"; headline += "" + 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 = "

"+OSRM.loc("YOUR_ROUTE_IS_BEING_COMPUTED")+".

"; -} -function showNoRouteDescription() { +}, +showNoRouteDescription: function() { headline = OSRM.loc("ROUTE_DESCRIPTION")+":
"; headline += "" + OSRM.loc("DISTANCE")+": N/A" @@ -224,11 +226,11 @@ function showNoRouteDescription() { document.getElementById('information-box-headline').innerHTML = headline; document.getElementById('information-box').innerHTML = "

"+OSRM.loc("YOUR_ROUTE_IS_BEING_COMPUTED")+".

"; -} +}, // 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