Home > Back-end >  Tweaking publicly-available Travel Time script to allow Waypoints
Tweaking publicly-available Travel Time script to allow Waypoints

Time:12-23

I am trying to add to a publicly-available script to give it a bit more functionality, but I keep getting an "invalid argument: address" error.

The original script, which does what it advertises:

/**
* Get Distance between 2 different addresses.
* @param start_address Address as string Ex. "300 N LaSalles St, Chicago, IL"
* @param end_address Address as string Ex. "900 N LaSalles St, Chicago, IL"
* @param return_type Return type as string Ex. "miles" or "kilometers" or "minutes" or "hours"
* @customfunction
*/

function GOOGLEMAPS(start_address,end_address,return_type) {

  // https://www.chicagocomputerclasses.com/
  // Nov 2017
  // improvements needed
  
  var mapObj = Maps.newDirectionFinder();
  mapObj.setOrigin(start_address);
  mapObj.setDestination(end_address);
  var directions = mapObj.getDirections();
  
  var getTheLeg = directions["routes"][0]["legs"][0];
  
  var meters = getTheLeg["distance"]["value"];
  
  switch(return_type){
    case "miles":
      return meters * 0.000621371;
      break;
    case "minutes":
        // get duration in seconds
        var duration = getTheLeg["duration"]["value"];
        //convert to minutes and return
        return duration / 60;
      break;
    case "hours":
        // get duration in seconds
        var duration = getTheLeg["duration"]["value"];
        //convert to hours and return
        return duration / 60 / 60;
      break;      
    case "kilometers":
      return meters / 1000;
      break;
    default:
      return "Error: Wrong Unit Type";
   }
  
}

My tweaked version, which simply adds the arguments for up to 4 waypoints and then adds them into the mapObj variable used to get the directions.

function GOOGLEMAPS(start_address,end_address, return_type, waypoint1, waypoint2, waypoint3, waypoint4) {

  // https://www.chicagocomputerclasses.com/
  // Nov 2017
  // improvements needed
  
  var mapObj = Maps.newDirectionFinder();
  mapObj.setOrigin(start_address);
  mapObj.setDestination(end_address);
  if(waypoint1 !== null ){mapObj.addWaypoint(waypoint1);}
  if(waypoint2 !== null ){mapObj.addWaypoint(waypoint2);}
  if(waypoint3 !== null ){mapObj.addWaypoint(waypoint3);}
  if(waypoint4 !== null ){mapObj.addWaypoint(waypoint4);}
  var directions = mapObj.getDirections();
  
  var getTheLeg = directions["routes"][0]["legs"][0];
  
  var meters = getTheLeg["distance"]["value"];
  
  switch(return_type){
    case "miles":
      return meters * 0.000621371;
      break;
    case "minutes":
        // get duration in seconds
        var duration = getTheLeg["duration"]["value"];
        //convert to minutes and return
        return duration / 60;
      break;
    case "hours":
        // get duration in seconds
        var duration = getTheLeg["duration"]["value"];
        //convert to hours and return
        return duration / 60 / 60;
      break;      
    case "kilometers":
      return meters / 1000;
      break;
    default:
      return "Error: Wrong Unit Type";
   }
  
}

The line giving the error is this added one. The if clause is my attempt at making the argument optional, as I'll be using this on lists of waypoints of varying length. if(waypoint1 !== null ){mapObj.addWaypoint(waypoint1);}

CodePudding user response:

For example, in your script, when the value of waypoint1 is not given as the argument of GOOGLEMAPS, the value becomes undefined. In this case, waypoint1 !== null is true. I thought that this might be the reason for your issue of invalid argument: address because in this case, mapObj.addWaypoint(waypoint1) is run with undefined.

If you want to run mapObj.addWaypoint(waypoint1) when waypoint1 has the value, how about the following modification?

From:

if(waypoint1 !== null ){mapObj.addWaypoint(waypoint1);}
if(waypoint2 !== null ){mapObj.addWaypoint(waypoint2);}
if(waypoint3 !== null ){mapObj.addWaypoint(waypoint3);}
if(waypoint4 !== null ){mapObj.addWaypoint(waypoint4);}

To:

if (waypoint1 !== undefined) { mapObj.addWaypoint(waypoint1); }
if (waypoint2 !== undefined) { mapObj.addWaypoint(waypoint2); }
if (waypoint3 !== undefined) { mapObj.addWaypoint(waypoint3); }
if (waypoint4 !== undefined) { mapObj.addWaypoint(waypoint4); }

or

if (waypoint1) { mapObj.addWaypoint(waypoint1); }
if (waypoint2) { mapObj.addWaypoint(waypoint2); }
if (waypoint3) { mapObj.addWaypoint(waypoint3); }
if (waypoint4) { mapObj.addWaypoint(waypoint4); }

References:

  • Related