-2

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

sinaraheneba
  • 781
  • 4
  • 18
Alex F
  • 13
  • 4
  • In order to correctly confirm your situation, can you provide the script for replicating your current issue? If your personal information is included, of course, please replace them. – Tanaike Jul 22 '19 at 23:38
  • Hi @Tanaike I've uploaded the entire script. Thank you for your willingness to take a look! – Alex F Jul 23 '19 at 02:22
  • Shouldn't `avoid2` be `...TOLLS`, rather than a string? Have you checked `setMode()` to ensure that you aren't receiving directions via e.g. public transport? Have you checked that the route given does in fact give highways, and that these highways are properly labeled as such on Google Maps? – sinaraheneba Jul 23 '19 at 04:14
  • Testing this, it appears only one `Avoid` can be set at a time. e.g. testing in `DRIVING` mode, `getDirections()` from Times Square to the Space Needle gives 1 day 18 hours. Settings `setAvoid("highways")` and running `getDirections()` again takes 2 days 8 hours. `setAvoid("tolls")` and running `getDirections()` once again puts the time back to 1 day 20 hours. It seems likely that you are accidentally putting it in tolls mode on a route which does not have toll roads. – sinaraheneba Jul 23 '19 at 04:36
  • @Alex F I noticed that an answer has already posted. I think that it will resolve your issue. – Tanaike Jul 23 '19 at 06:45
  • For future reference, your code must be reproducible. See [mre]. You should've at least shown what `formObject` looks like. – TheMaster Jul 24 '19 at 07:44

1 Answers1

2

Only one mode of setAvoid() can be set at a time, i.e., it is possible to avoid highways OR tolls, but not both. Workarounds for this exist within the API, but from what I've tested don't seem to be available in Apps Script.

Some testing:

var directions = Maps.newDirectionFinder()
  .setOrigin('Times Square, New York, NY')
  .setDestination('Space Needle, Seattle, WA')
Logger.log(directions.getDirections().routes[0].legs[0].duration.text) //1 day 18 hours

directions.setAvoid("highways")
Logger.log(directions.getDirections().routes[0].legs[0].duration.text) //2 days 8 hours

directions.setAvoid("tolls")
Logger.log(directions.getDirections().routes[0].legs[0].duration.text) //1 day 20 hours

This implies that either your if statements should be:

  if (formObject.avoid1 == "yes"){
    mapObj.setAvoid(Maps.DirectionFinder.Avoid.HIGHWAYS);
  } else if (formObject.avoid2 == "yes"){
    mapObj.setAvoid(Maps.DirectionFinder.Avoid.TOLLS);
    }

(Prioritizes avoiding highways), or alternatively, your form should only include a single option for avoid (e.g., mapObj.setAvoid(formObj.avoid);). With your code as given, it's likely that avoid2 being "yes" causes setAvoid() to be set to avoid tolls, which may not be a factor along your route.

sinaraheneba
  • 781
  • 4
  • 18
  • `from what I've tested don't seem to be available in Apps Script.`. ....So, `setAvoid("highways|tolls")` didn't work? – TheMaster Jul 23 '19 at 11:32
  • 1
    I specifically remember trying `"tolls|highways"` and it returned an error; I believe I tried `"highways|tolls"` and it didn't work, but I may have forgotten that one. Also tried using `%7C`, `+`, and some other random characters to see if anything would take, no dice. – sinaraheneba Jul 23 '19 at 11:38
  • That was it, I had to choose between one or the other. Thank you for your help!!!! – Alex F Jul 23 '19 at 20:42