js lat long distance

Solutions on MaxInterview for js lat long distance by the best coders in the world

showing results for - "js lat long distance"
Lylia
25 Feb 2018
1alert(calcCrow(59.3293371,13.4877472,59.3225525,13.4619422).toFixed(1));
2
3    //This function takes in latitude and longitude of two location and returns the distance between them as the crow flies (in km)
4    function calcCrow(lat1, lon1, lat2, lon2) {
5      var R = 6371; // km
6      var dLat = toRad(lat2-lat1);
7      var dLon = toRad(lon2-lon1);
8      var lat1 = toRad(lat1);
9      var lat2 = toRad(lat2);
10
11      var a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2);
12      var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
13      var d = R * c;
14      return d;
15    }
16
17    // Converts numeric degrees to radians
18    function toRad(Value) {
19        return Value * Math.PI / 180;
20    }