0

I'm using google apps script to code a distance finder for Google Maps. I've found examples of such, but they keep failing, so I thought I'd code my own. Sadly, this is failing with the same error:

TypeError: Cannot read property "legs" from undefined. (line 16).

It seems to be that it's sometimes working, and sometimes not. I have a few (3) places in my sheet that are calling the same functions, and at times one or more will return a valid response.

I saw elsewhere that people were suggesting using an API key to make sure that you get a good response, so that's what I've implemented below. (api keys redacted! is there a good way to tell if they've been recognised?)

Any ideas what might be going awry?!

Thanks in advance,

Mike

function mikeDistance(start, end){
  start = "CV4 8DJ";
  end = "cv4 9FE";

  var maps = Maps;
  maps.setAuthentication("#####", "#####");
  var dirFind = maps.newDirectionFinder();
  dirFind.setOrigin(start);
  dirFind.setDestination(end);

  var directions = dirFind.getDirections();
  var rawDistance = directions["routes"][0]["legs"][0]["distance"]["value"];
  var distance = rawDistance/1609.34;
  return distance;
}
MikeCoverUps
  • 723
  • 3
  • 10
  • 19
  • Try checking how the developer catches the error in this related [SO post](https://stackoverflow.com/a/30805183/5995040). This might help pinpoint the issue of undefined property. The developer logged each of the property of returned value of `getDirection()`: `directions["routes"][0]`, `directions["routes"][0].["legs"]`, he/she also add a period before calling the next property. Hope this helps. – Mr.Rebot May 09 '18 at 16:18
  • Thanks. That's a nice idea, and something I had tried. The problem seem to be that sometimes it works, sometimes it doesn't. Literally the same code will sometimes run, and then I hit run again and it fails! So infuriating! – MikeCoverUps May 09 '18 at 20:21
  • In my environment, your script works. Although I'm not sure whether this is useful for your situation, for example, can you try to remove ``maps.setAuthentication("#####", "#####");`` and run again? – Tanaike May 09 '18 at 22:37
  • I've tried that. I've now set up 2 cells with identical formulae. `=mikeDistance(B1,B2)` - sometimes one works, sometimes the other, sometimes neither... Authentication doesn't seem to make any difference. – MikeCoverUps May 10 '18 at 10:06
  • My solution is now to make a sheet that calls it a bunch of times (10 or so) down a whole row, then take the answers that aren't an error... It's super hacky, but it seems to yield a result... It's also super slow, but hey, it works for now. – MikeCoverUps May 10 '18 at 10:21
  • And now that doesn't work, because I've invoked it too many times for one day! Argh! It's impossible! – MikeCoverUps May 11 '18 at 15:44
  • It says I should be 2500 calls, but most of those calls have returned an error today, so not very helpful! I'm now wondering if they want me to use the Distance Matrix API, but that doesn't seem to work through google Apps Script. – MikeCoverUps May 11 '18 at 16:36

1 Answers1

1

Here's my short term solution while the issue is being fixed.

Not ideal, but at least reduces using your API limit as much as possible.

function getDistance(start, end) {

 return hackyHack(start, end, 0);
}

function hackyHack(start, end, level) {
  if (level > 5) {
    return "Error :(";
  }
  var directions = Maps.newDirectionFinder()
     .setOrigin(start)
     .setDestination(end)
     .setMode(Maps.DirectionFinder.Mode.DRIVING)
     .getDirections();
  var route = directions.routes[0];

  if (!route) return hackyHack(start, end, level+1); // Hacky McHackHack

  var distance = route.legs[0].distance.text;
  // var time = route.legs[0].duration.text;
  return distance;
}
Matt D
  • 26
  • 2
  • I've ended up using this, and it's fine. It seems like a problem with Google's API, but essentially it gets me up and running for now! Thanks! – MikeCoverUps May 23 '18 at 11:14