Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I spent about 15 minutes trying to create a custom Distance function in Google Sheets:

    const DISTANCE = function(a, b) {
      [latA, lonA] = a.split(",");
      [latB, lonB] = b.split(",");
      latA=parseFloat(latA);
      lonA=parseFloat(lonA);
      latB=parseFloat(latB);
      lonB=parseFloat(lonB);
  
      return 2 * 6371000 * ASIN(SQRT((SIN((latB*(3.14159/180)-latA*(3.14159/180))/2))^2+COS(latB*(3.14159/180))*COS(latA*(3.14159/180))*SIN(((lonB*(3.14159/180)-lonA*(3.14159/180))/2))^2))
    }
So that I could just paste concatenated "lat,lon" coords in place of the AWS region names and just compute e.g.

    =DISTANCE(A2,B1)
Into a second sheet, and then just divide the first sheet by the second sheet as a third sheet.

but I kept getting some "#NAME" error and I don't have time to figure it out.

Bad UX, Google. This should have been easy.

Better yet, you're freaking Google, why isn't there a DISTANCE("Hong Kong", "Singapore", "straightline") function?

I have to get back to work but if someone can figure the rest of this out please comment back.



If you’re going to calculate distances between two geological locations, then you want to calculate Great Circle distances, as that will be the shortest path at the surface of the planet.

A straight line distance might require tunneling down into the planet a surprising amount of distance, and is an absolute lower bound on the possible distance between the two points, but is not a realistic distance that could actually be achieved in most cases.


The formula above should be great circle distance, if I didn't screw it up.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: