Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

calculate distance between two coordinates formula javascript

alert(calcCrow(59.3293371,13.4877472,59.3225525,13.4619422).toFixed(1));



    //This function takes in latitude and longitude of two location and returns the distance between them as the crow flies (in km)
    function calcCrow(lat1, lon1, lat2, lon2) 
    {
      var R = 6371; // km
      var dLat = toRad(lat2-lat1);
      var dLon = toRad(lon2-lon1);
      var lat1 = toRad(lat1);
      var lat2 = toRad(lat2);

      var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
        Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2); 
      var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
      var d = R * c;
      return d;
    }

    // Converts numeric degrees to radians
    function toRad(Value) 
    {
        return Value * Math.PI / 180;
    }
Comment

js calculate distance between two coordinates

function calcCrow(lat1, lon1, lat2, lon2) {
    var R = 6371 // km
    var dLat = toRad(lat2 - lat1)
    var dLon = toRad(lon2 - lon1)
    var lat1 = toRad(lat1)
    var lat2 = toRad(lat2)

    var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
        Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2)
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))
    var d = R * c
    return d
}

// Converts numeric degrees to radians
function toRad(Value) {
    return Value * Math.PI / 180
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: img onerror 
Javascript :: react-native-youtube-iframe android crash 
Javascript :: js test letter lowercase 
Javascript :: javascript read input from terminal 
Javascript :: activate treeview menu in adminlte 3.0.2 treeview-menu open 
Javascript :: Uncaught TypeError: this is undefined ApolloClient ApolloClient.ts:72 
Javascript :: js does array.map maintain the order 
Javascript :: js after now 
Javascript :: native base expo web eror 
Javascript :: NotYetImplemented ng2-chart angular 2 
Javascript :: convert responsetext to json python 
Javascript :: how to wait in js 
Javascript :: javascript get div x y position 
Javascript :: how to get greatest common divisor in javascript 
Javascript :: javascript button onclick 
Javascript :: react how to scroll to element 
Javascript :: convert firebase timestamp to date js 
Javascript :: get top n objects from list node js 
Javascript :: widget is not working in arcgis map javascript 
Javascript :: default error handler express 
Javascript :: react native navigation back 
Javascript :: first remove active class from classlist and append to current element using javascript 
Javascript :: javascript set date to timezone 
Javascript :: load js after ajax 
Javascript :: saturn range in angular display end date 
Javascript :: react js set default route 
Javascript :: push elements to an object 
Javascript :: curl accept json 
Javascript :: map through keys javascript 
Javascript :: javascript create variable containing an object that will contain three properties that store the length of each side of the box 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =