/* This program is free software; you can redistribute it and/or modify it under the terms of the GNU AFFERO General Public License as published by the Free Software Foundation; either version 3 of the License, or any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA or see http://www.gnu.org/licenses/agpl.txt. */ // OSRM routing routines // [management of routing/direction requests and processing of responses] // [TODO: major refactoring scheduled] // some variables OSRM.GLOBALS.route = null; OSRM.GLOBALS.markers = null; OSRM.CONSTANTS.NO_DESCRIPTION = 0; OSRM.CONSTANTS.FULL_DESCRIPTION = 1; OSRM.G.dragging = null; OSRM.GLOBALS.dragid = null; OSRM.GLOBALS.pending = false; OSRM.GLOBALS.pendingTimer = null; // init routing data structures function initRouting() { OSRM.G.route = new OSRM.Route(); OSRM.G.markers = new OSRM.Markers(); } // -- JSONP processing -- // process JSONP response of routing server function timeoutRouteSimple() { showNoRouteGeometry(); showNoRouteDescription(); document.getElementById('information-box').innerHTML = "

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

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

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

"; } function showRouteSimple(response) { if(!response) return; if( !OSRM.G.dragging ) // prevent simple routing when no longer dragging return; if( response.status == 207) { showNoRouteGeometry(); showNoRouteDescription(); document.getElementById('information-box').innerHTML = "

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

"; } else { showRouteGeometry(response); showRouteDescriptionSimple(response); } 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); // } } function showRoute(response) { if(!response) return; if(response.status == 207) { showNoRouteGeometry(); OSRM.G.route.hideUnnamedRoute(); showNoRouteDescription(); document.getElementById('information-box').innerHTML = "

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

"; } else { showRouteGeometry(response); showRouteNonames(response); showRouteDescription(response); snapRoute(); } updateHints(response); } // show route geometry function showNoRouteGeometry() { var positions = []; for(var i=0; i'+response.ShortURL+']'; } function showRouteLink_TimeOut(){ document.getElementById('route-prelink').innerHTML = '['+OSRM.loc("LINK_TO_ROUTE_TIMEOUT")+']'; } function showRouteDescription(response) { // compute query string var query_string = '?rebuild=1'; for(var i=0; i'+OSRM.loc("GET_LINK_TO_ROUTE")+']'; // create GPX link var gpx_link = '['+OSRM.loc("GPX_FILE")+']'; // create route description var route_desc = ""; route_desc += ''; for(var i=0; i < response.route_instructions.length; i++){ //odd or even ? var rowstyle='results-odd'; if(i%2==0) { rowstyle='results-even'; } route_desc += ''; 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] ); if( response.route_instructions[i][1] != "" ) { route_desc += ' on '; route_desc += '' + response.route_instructions[i][1] + ''; } //route_desc += ' for '; route_desc += ''; route_desc += "'; if( i != response.route_instructions.length-1 ) route_desc += ''+OSRM.Utils.metersToDistance(response.route_instructions[i][2])+''; route_desc += "
'; headline = ""; headline += OSRM.loc("ROUTE_DESCRIPTION")+":
"; headline += '

'; headline += "" + OSRM.loc("DISTANCE")+": " + OSRM.Utils.metersToDistance(response.route_summary.total_distance) + "
" + OSRM.loc("DURATION")+": " + OSRM.Utils.secondsToTime(response.route_summary.total_time) + "
"; headline += '
'; headline += '
'+route_link+'
'+gpx_link+'
'; var output = ""; output += route_desc; document.getElementById('information-box-headline').innerHTML = headline; document.getElementById('information-box').innerHTML = output; } function showRouteDescriptionSimple(response) { headline = OSRM.loc("ROUTE_DESCRIPTION")+":
"; headline += "" + OSRM.loc("DISTANCE")+": " + OSRM.Utils.metersToDistance(response.route_summary.total_distance) + "
" + OSRM.loc("DURATION")+": " + OSRM.Utils.secondsToTime(response.route_summary.total_time) + "
"; headline += '

'; document.getElementById('information-box-headline').innerHTML = headline; document.getElementById('information-box').innerHTML = "

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

"; } function showNoRouteDescription() { headline = OSRM.loc("ROUTE_DESCRIPTION")+":
"; headline += "" + OSRM.loc("DISTANCE")+": N/A" + "
" + OSRM.loc("DURATION")+": N/A" + "
"; headline += '

'; 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) { // do not display unnamed streets? if( document.getElementById('option-highlight-nonames').checked == false) { OSRM.G.route.hideUnnamedRoute(); return; } // mark geometry positions where unnamed/named streets switch var named = []; for (var i = 0; i < response.route_instructions.length; i++) { if( response.route_instructions[i][1] == '' ) named[ response.route_instructions[i][3] ] = false; // no street name else named[ response.route_instructions[i][3] ] = true; // yes street name } // aggregate geometry for unnamed streets var geometry = decodeRouteGeometry(response.route_geometry, 5); var is_named = true; var current_positions = []; var all_positions = []; for( var i=0; i < geometry.length; i++) { current_positions.push( new L.LatLng(geometry[i][0], geometry[i][1]) ); // still named/unnamed? if( (named[i] == is_named || named[i] == undefined) && i != geometry.length-1 ) continue; // switch between named/unnamed! if(is_named == false) all_positions.push( current_positions ); current_positions = []; current_positions.push( new L.LatLng(geometry[i][0], geometry[i][1]) ); is_named = named[i]; } // display unnamed streets OSRM.G.route.showUnnamedRoute(all_positions); } //-- main function -- // generate server calls to query routes function getRoute(do_description) { // if source or target are not set -> hide route if( OSRM.G.markers.route.length < 2 ) { OSRM.G.route.hideRoute(); return; } // prepare JSONP call var type = null; var callback = null; var timeout = null; var source = OSRM.DEFAULTS.HOST_ROUTING_URL; source += '?z=' + OSRM.G.map.getZoom() + '&output=json' + '&geomformat=cmp'; if(OSRM.G.markers.checksum) source += '&checksum=' + OSRM.G.markers.checksum; for(var i=0; i= 0x20); var dlat = ((result & 1) ? ~(result >> 1) : (result >> 1)); lat += dlat; shift = 0; result = 0; do { b = encoded.charCodeAt(index++) - 63; result |= (b & 0x1f) << shift; shift += 5; } while (b >= 0x20); var dlng = ((result & 1) ? ~(result >> 1) : (result >> 1)); lng += dlng; array.push([lat * precision, lng * precision]); } return array; } // update hints of all markers function updateHints(response) { var hint_locations = response.hint_data.locations; OSRM.G.markers.checksum = response.hint_data.checksum; for(var i=0; i 1){ OSRM.G.markers.route[0].label = OSRM.C.SOURCE_LABEL; OSRM.G.markers.route[0].marker.setIcon( new L.Icon('images/marker-source.png') ); OSRM.G.markers.route[OSRM.G.markers.route.length-1].label = OSRM.C.TARGET_LABEL; OSRM.G.markers.route[OSRM.G.markers.route.length-1].marker.setIcon( new L.Icon('images/marker-target.png') ); } // recompute route if( OSRM.G.route.isShown() ) { 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) { if( OSRM.JSONP.fences["geocoder_source"] || OSRM.JSONP.fences["geocoder_target"] ) return; if( marker_id == OSRM.C.SOURCE_LABEL && OSRM.G.markers.hasSource() ) 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) { 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); }