added parameter to set meters/miles,

changed toHumanDistance functions so that conditions don't need to be
checked all the time
This commit is contained in:
shiin 2012-05-06 19:10:43 +02:00
parent 7451cc3150
commit 826cbc9a24
4 changed files with 33 additions and 17 deletions

View File

@ -24,7 +24,7 @@ OSRM.GUI.extend( {
// init
init: function() {
// init variables
OSRM.G.DISTANCE_FORMAT = OSRM.DEFAULTS.DISTANCE_FORMAT;
OSRM.Utils.setToHumanDistanceFunction(OSRM.DEFAULTS.DISTANCE_FORMAT);
// init events
document.getElementById("gui-input-source").onchange = function() {OSRM.GUI.inputChanged(OSRM.C.SOURCE_LABEL);};

View File

@ -165,6 +165,12 @@ OSRM.parseParameters = function(){
if(name_val[0] == 'hl') {
OSRM.Localization.setLanguage(name_val[1]);
}
else if(name_val[0] == 'df') {
var type = parseInt(name_val[1]);
if(type != 0 && type != 1)
return;
OSRM.Utils.setToHumanDistanceFunction(type);
}
else if(name_val[0] == 'loc') {
var coordinates = unescape(name_val[1]).split(',');
if(coordinates.length!=2 || !OSRM.Utils.isLatitude(coordinates[0]) || !OSRM.Utils.isLongitude(coordinates[1]) )

View File

@ -31,6 +31,7 @@ onClickRouteDescription: function(geometry_index) {
},
onClickCreateShortcut: function(src){
src += '&z='+ OSRM.G.map.getZoom() + '&center=' + OSRM.G.map.getCenter().lat.toFixed(6) + ',' + OSRM.G.map.getCenter().lng.toFixed(6);
src += '&df=' + OSRM.G.DISTANCE_FORMAT;
var source = OSRM.DEFAULTS.SHORTENER_PARAMETERS.replace(/%url/, OSRM.DEFAULTS.HOST_SHORTENER_URL+src);

View File

@ -31,30 +31,39 @@ toHumanTime: function(seconds){
hours = parseInt(minutes/60);
minutes = minutes%60;
if(hours==0){
return minutes + ' ' + 'min';
return minutes + ' ' + 'min';
}
else{
return hours + ' ' + 'h' + ' ' + minutes + ' ' + 'min';
return hours + ' ' + 'h' + ' ' + minutes + ' ' + 'min';
}
},
//human readable distance
toHumanDistance: function(meters){
setToHumanDistanceFunction: function(type) {
OSRM.G.DISTANCE_FORMAT = type;
if( type == 1 )
OSRM.Utils.toHumanDistance = OSRM.Utils.toHumanDistanceMiles;
else
OSRM.Utils.toHumanDistance = OSRM.Utils.toHumanDistanceMeters;
},
toHumanDistanceMeters: function(meters){
var distance = parseInt(meters);
if(OSRM.G.DISTANCE_FORMAT == 1) { // miles
distance = distance / 1609.344;
if(distance >= 100){ return (distance).toFixed(0)+' ' + 'mi'; }
else if(distance >= 10){ return (distance).toFixed(1)+' ' + 'mi'; }
else if(distance >= 0.1){ return (distance).toFixed(2)+' ' + 'mi'; }
else{ return (distance*5280).toFixed(0)+' ' + 'ft'; }
} else { // default to km, m
distance = distance / 1000;
if(distance >= 100){ return (distance).toFixed(0)+' ' + 'km'; }
else if(distance >= 10){ return (distance).toFixed(1)+' ' + 'km'; }
else if(distance >= 0.1){ return (distance).toFixed(2)+' ' + 'km'; }
else{ return (distance*1000).toFixed(0)+' ' + 'm'; }
}
distance = distance / 1000;
if(distance >= 100){ return (distance).toFixed(0)+' ' + 'km'; }
else if(distance >= 10){ return (distance).toFixed(1)+' ' + 'km'; }
else if(distance >= 0.1){ return (distance).toFixed(2)+' ' + 'km'; }
else{ return (distance*1000).toFixed(0)+' ' + 'm'; }
},
toHumanDistanceMiles: function(meters){
var distance = parseInt(meters);
distance = distance / 1609.344;
if(distance >= 100){ return (distance).toFixed(0)+' ' + 'mi'; }
else if(distance >= 10){ return (distance).toFixed(1)+' ' + 'mi'; }
else if(distance >= 0.1){ return (distance).toFixed(2)+' ' + 'mi'; }
else{ return (distance*5280).toFixed(0)+' ' + 'ft'; }
},
toHumanDistance: null,
// [verification routines]