0

Here is my code. For some reason Sheets is saying that the "Split function is not recognized".

function distance3(latlon1, latlon2){
  var [lat1, lon1] = split(latlon1,",");
  var [lat2, lon2] = split(latlon2,",");
  var R = 6371000; // radius of the earth in meters, https://en.wikipedia.org/wiki/Earth_radius
  var dLat = (lat2-lat1) * Math.PI / 180; // Convert degrees to radians
  var dLon = (lon2-lon1) * Math.PI / 180; // Convert degrees to radians
  var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
          Math.cos(lat1 * Math.PI / 180 ) * Math.cos(lat2 * Math.PI / 180 ) *
          Math.sin(dLon/2) * Math.sin(dLon/2);
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
  var d = R * c;

  // Distance in meters, rounded to an integer.
  return Math.round(d)*0.000621371;
}

THANK YOU!

1
  • Check the Javascript documentation on the split function. reference Commented Dec 2, 2019 at 19:27

1 Answer 1

1

Try:

var [lat1, lon1] = latlon1.split(",");
Sign up to request clarification or add additional context in comments.

3 Comments

Giving me TypeError: Cannot call method "split" of undefined. (line 2, file "distance")
This means one of your vars, latlon1 or latlon2 is undefined. if you call .split on a string it works. try "1,2".split(",") for instance. You should check to see why your function arguments are undefined then
Please show how you are calling the function. My guess is that latlon1, and latlon2 are not arrays. They're probably object with key/value pairs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.