I receive the same duration & distance whether or not I am expecting to avoid highways or tolls
I suspect the issue is either in my order of operations, or the way I am calculating distance & duration
I have tried setting up setAvoid in two different ways (see code), as well as changed my if statements to accommodate what the HTML form (check box) might return.
I have also tried switching the if statement to "return formObject.avoid1" this showed me that the if statement is functioning properly - so either the .setAvoid is not working the way I have written it, or the distance & duration calculations are not considering the setAvoid
function processForm(formObject) {
var allDestinations = [formObject.destination1, formObject.destination2, formObject.destination3, formObject.destination4];
var allDest = [formObject.dest1, formObject.dest2, formObject.dest3, formObject.dest4];
var numWaypoints = 0;
var waypoints = [];
for (var i=0; i<=allDestinations.length; ++i){
if (allDestinations[i] == "TRUE"){
numWaypoints ++;
waypoints.push(allDest[i]);
}
}
var mapObj = Maps.newDirectionFinder();
mapObj.setOrigin(formObject.origin)
.setDestination('my address, never changes');
//the purpose of legOne is to get the distance & duration from me to the input origin. I will always be the first and last stop. I do it this way because the input origin always needs to be the first stop. This prevents 'optimize waypoints' from altering the input origin.
var legOne = mapObj.getDirections();
var totalDistance = legOne["routes"][0]["legs"][0]["distance"]["value"];
var totalDuration = legOne["routes"][0]["legs"][0]["duration"]["value"];
for (var cc=0; cc<numWaypoints; ++cc){
mapObj.addWaypoint(waypoints[cc]);
}
if (formObject.optimize == "true"){
mapObj.setOptimizeWaypoints(true);
}
if (formObject.avoid1 == "yes"){
mapObj.setAvoid(Maps.DirectionFinder.Avoid.HIGHWAYS);
}
if (formObject.avoid2 == "yes"){
mapObj.setAvoid("tolls");
}
var directions = mapObj.getDirections();
var bestRoute = directions["routes"][0];
var numLegs = bestRoute["legs"].length;
for (var c=0; c<numLegs; ++c){
var legNum = directions["routes"][0]["legs"][c];
var legDistance = legNum["distance"]["value"];
var legDuration = legNum["duration"]["value"];
totalDistance += legDistance;
totalDuration += legDuration;
}
var miles = totalDistance * 0.000621371;
var driveTime = totalDuration / 60 / 60;
var numPallets = formObject.numPallets;
var parkTime = numWaypoints * (1/6);
var onOffLoadTime = numPallets * .5;
var totalTime = driveTime + parkTime + onOffLoadTime;
var dataArray = [miles, totalTime, driveTime, numPallets, onOffLoadTime, numWaypoints, parkTime];
return dataArray;
}
If we are in fact avoiding highways, I expect the duration to increase, but I get the same duration whether or not we avoid highways