2011-05-15 15:53:09 -04:00
/ *
* Open Source Routing Machine ( OSRM ) - Web ( GUI ) Interface
* Copyright ( C ) Pascal Neis , 2011
*
* 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.
* /
/ * *
* Title : Route . js
* Description : JS file for routing
*
* @ author Pascal Neis , pascal @ neis - one . org
2011-06-30 07:35:08 -04:00
* @ version 0.2 2011 - 06 - 23
2011-05-15 15:53:09 -04:00
* /
//======================
// OBJECTS
//Map
2011-07-22 12:58:05 -04:00
//var HOST_ROUTING_URL = 'http://localhost:5000/viaroute';
var HOST _ROUTING _URL = 'http://routingdemo.geofabrik.de/route-via/' ;
2011-06-30 07:35:08 -04:00
var HOST _WEBSITE = 'http://map.project-osrm.org/' ; //location.host
2011-07-22 12:15:42 -04:00
2011-05-15 15:53:09 -04:00
var ISCALCULATING = false ;
var EPSG _4326 = new OpenLayers . Projection ( "EPSG:4326" ) ;
var EPSG _900913 = new OpenLayers . Projection ( "EPSG:900913" ) ;
2011-07-22 12:15:42 -04:00
var allRoutePoints = [ ] ;
2011-05-15 15:53:09 -04:00
//======================
// FUNCTIONS
/ *
* routing ( ) - Function to create and send route request
* /
function routing ( isDragRoute ) {
//Check if startpoint is set
if ( ! isStartPointSet ) {
//alert("Please set your Start location first!");
document . getElementById ( 'information' ) . innerHTML = '<p class="infoHLRed">Please set your Start location first!</p>' ;
return ;
}
//Check if endpoint is set
if ( ! isEndPointSet ) {
//alert("Please set your Destination first!");
document . getElementById ( 'information' ) . innerHTML = '<p class="infoHLRed">Please set your Destination first!</p>' ;
return ;
}
//Check if already a route is planning
if ( ! isDragRoute ) { ISCALCULATING = false ; }
if ( ISCALCULATING ) { return ; }
else { ISCALCULATING = true ; }
//Get Coordinates of the Start and Endpoint
var startFeat = getMarkerByName ( 'start' ) ;
var endFeat = getMarkerByName ( 'end' ) ;
var from = new OpenLayers . LonLat ( startFeat . geometry . x , startFeat . geometry . y ) . transform ( new OpenLayers . Projection ( "EPSG:900913" ) , new OpenLayers . Projection ( "EPSG:4326" ) ) ;
var to = new OpenLayers . LonLat ( endFeat . geometry . x , endFeat . geometry . y ) . transform ( new OpenLayers . Projection ( "EPSG:900913" ) , new OpenLayers . Projection ( "EPSG:4326" ) ) ;
//Send Request
var script = document . createElement ( 'script' ) ;
script . type = 'text/javascript' ;
var callBackFunction = 'showResultsRoute' ;
2011-07-22 12:15:42 -04:00
var instructions = '&geomformat=cmp&instructions=true' ;
2011-05-15 15:53:09 -04:00
if ( isDragRoute ) {
callBackFunction = 'showResultsDragRoute' ;
2011-07-22 12:15:42 -04:00
instructions = '&geomformat=cmp&instructions=false' ;
2011-05-15 15:53:09 -04:00
document . getElementById ( 'information' ) . innerHTML = '<p class="infoHL">Release mouse button to get Route Information!</p>(If no Route Summary is diplayed, press the Route!-button)' ;
}
2011-07-22 12:15:42 -04:00
script . src = HOST _ROUTING _URL + "&start=" + from . lat + ',' + from . lon + '&dest=' + to . lat + ',' + to . lon ;
for ( var i = 0 ; i < viaPointsVector . length ; i ++ ) {
script . src += ( '&via=' + viaPointsVector [ i ] [ 0 ] + ',' + viaPointsVector [ i ] [ 1 ] ) ;
}
script . src += '&z=' + this . map . getZoom ( ) + '&output=json&jsonp=' + callBackFunction + instructions ;
2011-05-15 15:53:09 -04:00
document . body . appendChild ( script ) ;
}
/ *
* reroute ( ) - Function
* /
function reroute ( ) {
if ( ! isStartPointSet || ! isEndPointSet ) { return ; }
routing ( false ) ;
}
/ *
* showResultsDragRoute ( ) - Function to show route result for drag - route
* /
function showResultsDragRoute ( response ) {
if ( response ) {
//Display Route
showRouteGeometry ( response ) ;
}
2011-05-20 08:15:50 -04:00
markersLayer . clearMarkers ( ) ;
2011-05-15 15:53:09 -04:00
ISCALCULATING = false ;
}
/ *
* showResultsRoute ( ) - Function to show route result
* /
function showResultsRoute ( response ) {
2011-07-22 12:15:42 -04:00
if ( response ) {
//Display Route
2011-05-15 15:53:09 -04:00
if ( document . getElementById ( "cbNoNames" ) . checked == true ) {
showNoNameStreets ( response ) ;
}
else {
showRouteGeometry ( response ) ;
}
2011-06-19 15:13:07 -04:00
2011-07-22 12:15:42 -04:00
//Save Via Points that come with route
var lengthOfArray = response . via _points . length ;
var i = 0 ;
viaPointsVector . length = 0 ;
viaPointsVector = response . via _points . slice ( 0 ) ;
paintViaPoints ( ) ;
//Create Link of the route
2011-06-19 15:13:07 -04:00
var startFeat = getMarkerByName ( 'start' ) ;
var endFeat = getMarkerByName ( 'end' ) ;
var from = new OpenLayers . LonLat ( startFeat . geometry . x , startFeat . geometry . y ) . transform ( EPSG _900913 , EPSG _4326 ) ;
var to = new OpenLayers . LonLat ( endFeat . geometry . x , endFeat . geometry . y ) . transform ( EPSG _900913 , EPSG _4326 ) ;
2011-07-22 12:15:42 -04:00
var routelink = '<div id="routelink"><input name="routelink" type="submit" title="Get Link" onClick="createShortLink(\'' + HOST _WEBSITE + '?fr=' + from . lat . toFixed ( 6 ) + ',' + from . lon . toFixed ( 6 ) + '&to=' + to . lat . toFixed ( 6 ) + ',' + to . lon . toFixed ( 6 ) ;
for ( i = 0 ; i < viaPointsVector . length ; i ++ ) {
routelink += "&via=" + viaPointsVector [ i ] [ 0 ] + "," + viaPointsVector [ i ] [ 1 ] ;
}
routelink += '\');" value="Get Link"></div>' ;
//Link for the GPX Download
var gpxLink = '(<a href="' + HOST _ROUTING _URL + '&start=' + from . lat . toFixed ( 6 ) + ',' + from . lon . toFixed ( 6 ) + '&dest=' + to . lat . toFixed ( 6 ) + ',' + to . lon . toFixed ( 6 ) + '&z=' + this . map . getZoom ( ) ;
for ( i = 0 ; i < viaPointsVector . length ; i ++ ) {
gpxLink += "&via=" + viaPointsVector [ i ] [ 0 ] + "," + viaPointsVector [ i ] [ 1 ] ;
}
gpxLink += '&output=gpx">Get GPX File</a>)' ; ;
2011-05-15 15:53:09 -04:00
//Show Route Summary
var output = '<p class="routeSummaryHL">Some information about your Way <br> from \'<span class="routeSummaryHLlight">' + response . route _summary . start _point + '</span>\' to \'<span class="routeSummaryHLlight">' + response . route _summary . end _point + '</span>\'</p>' ;
2011-07-09 15:31:45 -04:00
output += '<p class="routeSummary">Distance: <span class="routeSummarybold">' + response . route _summary . total _distance / 1000 + ' km</span> - Duration: <span class="routeSummarybold">' + secondsToTime ( response . route _summary . total _time ) + '</span></p><p>' + routelink + '</p><p><span class="routeInstructionsHL">The Route-Instructions:</span> ' + gpxLink + '</p>' ;
2011-05-15 15:53:09 -04:00
//Show Route Instructions
2011-07-22 12:15:42 -04:00
output += '<table>' ;
lengthOfArray = response . route _instructions . length ;
for ( i = 0 ; i < lengthOfArray ; i ++ ) {
//console.log(response.route_instructions[i]);
2011-05-15 15:53:09 -04:00
//odd or even ?
var rowstyle = 'routeInstructionsOdd' ;
if ( i % 2 == 0 ) { rowstyle = 'routeInstructionsEven' ; }
2011-07-22 12:15:42 -04:00
2011-05-15 15:53:09 -04:00
var indexPos = response . route _instructions [ i ] [ 3 ] ;
2011-07-22 12:15:42 -04:00
//console.log('setting : ' + response.route_instructions[i] + ' at ' + allRoutePoints[indexPos]);
var point = allRoutePoints [ indexPos ] ;
2011-05-15 15:53:09 -04:00
output += '<tr class="' + rowstyle + '"><td align="right" valign="top"><span class="routeSummarybold">' + ( i + 1 ) + '.</span></td><td class="' + rowstyle + '"><a href="#" class="nolinkStyle" onclick="setMapCenter(new OpenLayers.LonLat(' + point . x + ',' + point . y + '));">' + response . route _instructions [ i ] [ 0 ] + ' on ' + response . route _instructions [ i ] [ 1 ] + ' for ' + getDistanceWithUnit ( response . route _instructions [ i ] [ 2 ] ) + '</a></td></tr>' ;
}
2011-07-22 12:15:42 -04:00
var rowstyle = 'routeInstructionsOdd' ;
if ( i % 2 == 0 ) { rowstyle = 'routeInstructionsEven' ; }
if ( lengthOfArray > 0 ) {
var point = allRoutePoints [ allRoutePoints . length - 1 ] ;
output += '<tr class="' + rowstyle + '"><td align="right" valign="top"><span class="routeSummarybold">' + ( i + 1 ) + '.</span></td><td class="' + rowstyle + '"><a href="#" class="nolinkStyle" onclick="setMapCenter(new OpenLayers.LonLat(' + point . x + ',' + point . y + '));">You have reached your destination</a></td></tr>' ;
}
output += '</table>' ;
2011-05-15 15:53:09 -04:00
//alert(vectorLayerRoute.features[0].geometry.getVertices());
2011-07-22 12:15:42 -04:00
document . getElementById ( 'information' ) . innerHTML = output ;
ISCALCULATING = false ;
ISCALCULATINGVIA = false ;
}
2011-05-15 15:53:09 -04:00
}
/ *
2011-07-22 12:15:42 -04:00
* showRouteGeometry ( ) - Function to show route result
2011-05-15 15:53:09 -04:00
* /
function showRouteGeometry ( response ) {
2011-07-22 12:15:42 -04:00
if ( response ) {
allRoutePoints . length = 0 ;
2011-06-30 07:35:08 -04:00
// now with compression of the route geometry
var geometry = decodeRouteGeometry ( response . route _geometry , 5 ) ;
var lengthOfArray = geometry . length ;
var points = [ ] ;
points . length = lengthOfArray ;
2011-07-22 12:15:42 -04:00
var points = [ ] ;
//delete any previously displayed via route
vectorLayerViaRoute . removeFeatures ( vectorLayerViaRoute . features ) ;
vectorLayerRoute . removeAllFeatures ( ) ;
//Create Route Layer for Display
for ( var i = 0 ; i < lengthOfArray ; i ++ ) {
var point = new OpenLayers . Geometry . Point ( geometry [ i ] [ 1 ] , geometry [ i ] [ 0 ] ) . transform ( EPSG _4326 , EPSG _900913 ) ;
allRoutePoints . push ( point ) ;
points . push ( point ) ;
2011-05-15 15:53:09 -04:00
if ( i % 1024 == 0 && i > 0 || i == lengthOfArray - 1 ) {
2011-07-22 12:15:42 -04:00
/ * v a r f e a t u r e 2 = n e w O p e n L a y e r s . F e a t u r e . V e c t o r ( n e w O p e n L a y e r s . G e o m e t r y . L i n e S t r i n g ( p o i n t s ) , n u l l , {
strokeColor : "#000066" ,
strokeOpacity : 1 ,
strokeWidth : 9
} ) ;
var feature1 = new OpenLayers . Feature . Vector ( new OpenLayers . Geometry . LineString ( points ) , null , {
strokeColor : "#ffffff" ,
strokeOpacity : 0.75 ,
strokeWidth : 7
} ) ;
* /
var feature = new OpenLayers . Feature . Vector ( new OpenLayers . Geometry . LineString ( points ) , null , {
2011-05-15 15:53:09 -04:00
strokeColor : "#0033ff" ,
strokeOpacity : 0.7 ,
strokeWidth : 5
2011-07-22 12:15:42 -04:00
} ) ;
// vectorLayerRoute.addFeatures([feature2]);
// vectorLayerRoute.addFeatures([feature1]);
vectorLayerRoute . addFeatures ( [ feature ] ) ;
points = [ ] ;
points . push ( point ) ;
}
}
2011-05-15 15:53:09 -04:00
}
}
/ *
* showNoNameStreets ( ) - Function to show route result
* /
function showNoNameStreets ( response ) {
2011-07-22 12:15:42 -04:00
if ( response ) {
allRoutePoints . length = 0 ;
vectorLayerRoute . removeFeatures ( vectorLayerRoute . features ) ;
// now with compression of the route geometry
2011-06-30 07:35:08 -04:00
var geometry = decodeRouteGeometry ( response . route _geometry , 5 ) ;
var lengthOfArray = geometry . length ;
2011-07-22 12:15:42 -04:00
var points = [ ] ;
points . length = lengthOfArray ;
2011-05-15 15:53:09 -04:00
//Check if a instruction has no name !
var colors = [ ] ;
if ( true ) {
var instrucLength = response . route _instructions . length ;
for ( var i = 0 ; i < instrucLength ; i ++ ) {
var indexPos = response . route _instructions [ i ] [ 3 ] ;
var streetName = response . route _instructions [ i ] [ 1 ] ;
if ( streetName == '' ) { colors [ indexPos ] = "#FF00FF" ; }
else { colors [ indexPos ] = "#0033ff" ; }
}
}
2011-07-22 12:15:42 -04:00
//delete any previously displayed via route
vectorLayerViaRoute . removeFeatures ( vectorLayerViaRoute . features ) ;
vectorLayerRoute . removeAllFeatures ( ) ;
2011-05-15 15:53:09 -04:00
//Create Route Layer for Display
var color = "#0033ff" ;
2011-06-30 07:35:08 -04:00
var changeColor = false ;
for ( var i = 0 ; i < lengthOfArray ; i ++ ) {
var point = new OpenLayers . Geometry . Point ( geometry [ i ] [ 1 ] , geometry [ i ] [ 0 ] ) . transform ( EPSG _4326 , EPSG _900913 ) ;
points . push ( point ) ;
2011-07-22 12:15:42 -04:00
allRoutePoints . push ( point ) ;
2011-05-15 15:53:09 -04:00
if ( colors [ i ] != undefined ) { changeColor = true ; }
if ( i % 1024 == 0 && i > 0 || i == lengthOfArray - 1 || changeColor ) {
var feature = new OpenLayers . Feature . Vector ( new OpenLayers . Geometry . LineString ( points ) , null , {
strokeColor : color ,
strokeOpacity : 0.7 ,
strokeWidth : 5
} ) ;
vectorLayerRoute . addFeatures ( [ feature ] ) ;
points = [ ] ;
points . push ( point ) ;
if ( colors [ i ] != undefined ) { color = colors [ i ] ; }
changeColor = false ;
}
}
}
}
2011-06-30 07:35:08 -04:00
/ *
* secondsToTime ( ) - Function to transform seconds to a time string
* /
2011-05-15 15:53:09 -04:00
function secondsToTime ( seconds ) {
seconds = parseInt ( seconds ) ;
minutes = parseInt ( seconds / 60 ) ;
seconds = seconds % 60 ;
hours = parseInt ( minutes / 60 ) ;
minutes = minutes % 60 ;
//return stunden+':'+minuten+':'+seconds;
if ( hours == 0 ) {
return minutes + ' min(s)' ;
}
else {
return hours + ' h ' + minutes + ' min(s)' ;
}
}
2011-06-30 07:35:08 -04:00
/ *
* getDistanceWithUnit ( ) - Function to return a distance with units
* /
2011-05-15 15:53:09 -04:00
function getDistanceWithUnit ( distance ) {
distance = parseInt ( distance ) ;
if ( distance >= 1000 ) { return ( parseInt ( distance / 1000 ) ) + ' km' ; }
else { return distance + ' m' ; }
}
2011-06-30 07:35:08 -04:00
/ *
* setMapCenter ( ) - Function to add a marker and center the map
* /
2011-05-15 15:53:09 -04:00
function setMapCenter ( lonlat ) {
2011-07-22 12:15:42 -04:00
//lonlat.transform(new OpenLayers.Projection("EPSG:4326"), new OpenLayers.Projection("EPSG:900913"));
//console.log('zooming to :' + lonlat);
2011-05-15 15:53:09 -04:00
//Add Marker
markersLayer . clearMarkers ( ) ;
var size = new OpenLayers . Size ( 21 , 25 ) ;
var offset = new OpenLayers . Pixel ( - ( size . w / 2 ) , - size . h ) ;
var icon = new OpenLayers . Icon ( 'img/marker.png' , size , offset ) ;
markersLayer . addMarker ( new OpenLayers . Marker ( lonlat , icon ) ) ;
2011-06-30 07:35:08 -04:00
2011-05-15 15:53:09 -04:00
//Hack - FIXME !
map . setCenter ( new OpenLayers . LonLat ( lonlat . lon - 200 , lonlat . lat ) , 17 ) ;
2011-06-30 07:35:08 -04:00
}
/ *
* decodeRouteGeometry ( ) - Function to decode encoded Route Geometry
* /
function decodeRouteGeometry ( encoded , precision ) {
precision = Math . pow ( 10 , - precision ) ;
var len = encoded . length , index = 0 , lat = 0 , lng = 0 , array = [ ] ;
while ( index < len ) {
var b , shift = 0 , result = 0 ;
do {
b = encoded . charCodeAt ( index ++ ) - 63 ;
result |= ( b & 0x1f ) << shift ;
shift += 5 ;
} while ( b >= 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 ;
}
2011-06-30 07:37:31 -04:00
/ *
* createShortLink ( ) - Function to "create" shortlink of a route
* /
2011-06-30 07:35:08 -04:00
function createShortLink ( str ) {
var script = document . createElement ( 'script' ) ;
script . type = 'text/javascript' ;
var callBackFunction = 'showRouteLink' ;
script . src = 'http://map.project-osrm.org/shorten/' + str + '&jsonp=showRouteLink' ;
document . body . appendChild ( script ) ;
}
2011-06-30 07:37:31 -04:00
/ *
* showRouteLink ( ) - Function
* /
2011-06-30 07:35:08 -04:00
function showRouteLink ( response ) {
document . getElementById ( 'routelink' ) . innerHTML = '<span class="routeSummarybold"> >> Your ShortLink:</span> <a href="' + response . ShortURL + '">' + response . ShortURL + '</a>' ;
}