Skip to content

Instantly share code, notes, and snippets.

@jnritchie
Created October 17, 2022 18:34
Show Gist options
  • Select an option

  • Save jnritchie/68be5d4515d6ee88b89dd4d9d3450007 to your computer and use it in GitHub Desktop.

Select an option

Save jnritchie/68be5d4515d6ee88b89dd4d9d3450007 to your computer and use it in GitHub Desktop.

Revisions

  1. jnritchie created this gist Oct 17, 2022.
    15 changes: 15 additions & 0 deletions distance-calc.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    var rad = function(x) {
    return x * Math.PI / 180;
    };

    var getDistance = function(p1, p2) {
    var R = 6378137; // Earth’s mean radius in meter
    var dLat = rad(p2.lat() - p1.lat());
    var dLong = rad(p2.lng() - p1.lng());
    var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
    Math.cos(rad(p1.lat())) * Math.cos(rad(p2.lat())) *
    Math.sin(dLong / 2) * Math.sin(dLong / 2);
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    var d = R * c;
    return d; // returns the distance in meter
    };