Calculate distance in kilometers from one condemned to another. Longitude, latitude (google Map) in Javascript

 We are going to create a function that allows us to know the distance in kilometers between two points on a map. This function can be very useful when it comes to calculating how many meters there are between one point and another, or if you have an application that needs to calculate which is the closest place.

 getKilometers = function(lat1,lon1,lat2,lon2) { rad = function(x) {return x*Math.PI/180;} var R = 6378.137; //Radius of the earth in km var dLat = rad( lat2 - lat1 ); var dLong = rad( lon2 - lon1); var a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(rad(lat1)) * Math.cos(rad(lat2)) * 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.toFixed(3); //Return three decimal places }

To use this function we only have to call it passing as parameters the latitude of the first point, the longitude of the first point, the latitude of the second point and the longitude of the second point.

This function can be very useful, for example, if we have several points with their coordinates that we extract from our own database and we want to filter only those that are at a certain distance from the point we are interested in, for example, those that are at 5 kilometers around the current coordinates. In this case, we could go through an Array with all the points and ask for those that are less than 5 kilometers away, as follows:

Let's imagine that we have an Array A with several points and their coordinates and we want to put in Array B the points that are less than 5 kilometers from the current coordinates, we could do something like this:

navigator.geolocation.getCurrentPosition(function(pos) { var lat = pos.coords.latitude; var lon = pos.coords.longitude; for (var i=0; i <A.length; i++)
 {
   if(parseInt(getKilometros(lat,lon,A[i].laitude,A[i].lon))<= 5
   {
     B.push(A[i]);
   }
 }
}

If you are asking to get the distance in meters, just change the variable R (Radius of the earth) to meters. eg

 var R = 6378137; //Radius of the earth in meters

In this way, the result of the function would be a distance in kilometers.

Examples in TypeScript

const getLocationMetros = (location1: location, location2: location): number => { const rad = function (x: number) { return x * Math.PI / 180; } //const R = 6378.137; //Radius of the earth in km const R = 6378137; //Radius of the earth in Meters const dLat = rad(location2.lat - location1.lat); const dLong = rad(location1.lng - location2.lng); const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(rad(location1.lat)) * Math.cos(rad(location2.lat)) * Math.sin(dLong / 2) * Math.sin(dLong / 2); const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); const d = R * c; return parseInt(d.toFixed(3)) }

In this way we obtain the distance between 2 points, in meters or kilometers.

Leave A Comment

Create your account

en_USEnglish