## Calculating distance between two points, using latitude longitude?

java code to find distance between two latitude longitude points
how to calculate distance between two latitude and longitude in excel
haversine formula
formula to calculate distance between two latitude and longitude
latitude/longitude distance calculator
calculate bearing between two coordinates latitude/longitude
calculate distance between two latitude/longitude points in meters
convert latitude and longitude to meters formula

Here's my try, it's just a snippet of my code:

```final double RADIUS = 6371.01;
double temp = Math.cos(Math.toRadians(latA))
* Math.cos(Math.toRadians((latB) - (latA)))
return temp * RADIUS * Math.PI / 180;
```

I am using this formulae to get the latitude and longitude:

```x = Deg + (Min + Sec / 60) / 60)
```

The Java code given by Dommer above gives slightly incorrect results but the small errors add up if you are processing say a GPS track. Here is an implementation of the Haversine method in Java which also takes into account height differences between two points.

```/**
* Calculate distance between two points in latitude and longitude taking
* into account height difference. If you are not interested in height
* difference pass 0.0. Uses Haversine method as its base.
*
* lat1, lon1 Start point lat2, lon2 End point el1 Start altitude in meters
* el2 End altitude in meters
* @returns Distance in Meters
*/
public static double distance(double lat1, double lat2, double lon1,
double lon2, double el1, double el2) {

final int R = 6371; // Radius of the earth

double latDistance = Math.toRadians(lat2 - lat1);
double lonDistance = Math.toRadians(lon2 - lon1);
double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2)
* Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double distance = R * c * 1000; // convert to meters

double height = el1 - el2;

distance = Math.pow(distance, 2) + Math.pow(height, 2);

return Math.sqrt(distance);
}
```

Latitude/Longitude Distance Calculator, Enter latitude and longitude of two points, select the desired units: nautical miles The calculations are approximate in nature and may differ a little from the  This tool calculates the straight line distance between two pairs of latitude/longitude points provide in decimal degrees. Additionally, this tool will convert your decimal coordinatesto degrees minutes seconds(DMS). South latitudes points are expressed in negative values, while east longitudes are positive values.

Edit

I found another reference to the code.

And, posted below, just in case it disappears again.

```    private double distance(double lat1, double lon1, double lat2, double lon2, char unit) {
double theta = lon1 - lon2;
dist = Math.acos(dist);
dist = dist * 60 * 1.1515;
if (unit == 'K') {
dist = dist * 1.609344;
} else if (unit == 'N') {
dist = dist * 0.8684;
}
return (dist);
}

/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*::  This function converts decimal degrees to radians             :*/
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
private double deg2rad(double deg) {
return (deg * Math.PI / 180.0);
}

/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*::  This function converts radians to decimal degrees             :*/
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
return (rad * 180.0 / Math.PI);
}

System.out.println(distance(32.9697, -96.80322, 29.46786, -98.53506, 'M') + " Miles\n");
System.out.println(distance(32.9697, -96.80322, 29.46786, -98.53506, 'K') + " Kilometers\n");
System.out.println(distance(32.9697, -96.80322, 29.46786, -98.53506, 'N') + " Nautical Miles\n");
```

Calculate distance and bearing between two Latitude/Longitude , Alternatively, the polar coordinate flat-earth formula can be used: using the co-​latitudes θ1 = π/2−φ1 and θ2 Rhumb-line distance between two points  As per Wikipedia, the Geographical distance between two points/places is the distance measured along the surface of the earth. This distance between two lat/longs is based on some level of abstraction and it’s not an exact distance, which is unattainable if you are considering all the irregularities on the surface of earth.

Note: this solution only works for short distances.

I tried to use dommer's posted formula for an application and found it did well for long distances but in my data I was using all very short distances, and dommer's post did very poorly. I needed speed, and the more complex geo calcs worked well but were too slow. So, in the case that you need speed and all the calculations you're making are short (maybe < 100m or so). I found this little approximation to work great. it assumes the world is flat mind you, so don't use it for long distances, it works by approximating the distance of a single Latitude and Longitude at the given Latitude and returning the Pythagorean distance in meters.

```public class FlatEarthDist {
//returns distance in meters
public static double distance(double lat1, double lng1,
double lat2, double lng2){
double a = (lat1-lat2)*FlatEarthDist.distPerLat(lat1);
double b = (lng1-lng2)*FlatEarthDist.distPerLng(lat1);
return Math.sqrt(a*a+b*b);
}

private static double distPerLng(double lat){
return 0.0003121092*Math.pow(lat, 4)
+0.0101182384*Math.pow(lat, 3)
-17.2385140059*lat*lat
+5.5485277537*lat+111301.967182595;
}

private static double distPerLat(double lat){
return -0.000000487305676*Math.pow(lat, 4)
-0.0033668574*Math.pow(lat, 3)
+0.4601181791*lat*lat
-1.4558127346*lat+110579.25662316;
}
}
```

Calculate distance between two latitude-longitude points , This link might be helpful to you, as it details the use of the Haversine formula to calculate the distance. Excerpt: This script [in Javascript] calculates great-circle  I'm trying to calculate distance between two points, using latitude longitude and altitude (elevation). I was using euklides formula in order to get my distance: D=√ ((Long1-Long2)²+ (Lat1-Lat2)²+ (Alt1-Alt2)²) My points are geographical coordinates and ofcourse altitude is my height above the sea.

Future readers who stumble upon this SOF article.

Obviously, the question was asked in 2010 and its now 2019. But it comes up early in an internet search. The original question does not discount use of third-party-library (when I wrote this answer).

```public double calculateDistanceInMeters(double lat1, double long1, double lat2,
double long2) {

double dist = org.apache.lucene.util.SloppyMath.haversinMeters(lat1, long1, lat2, long2);
return dist;
}
```

and

```<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-spatial</artifactId>
<version>8.2.0</version>
</dependency>
```

https://mvnrepository.com/artifact/org.apache.lucene/lucene-spatial/8.2.0

https://lucene.apache.org/core/8_2_0/core/org/apache/lucene/util/SloppyMath.html

Finding distances based on Latitude and Longitude, Formula (shown below) expressed in terms of a two-argument inverse tangent function to calculate the great circle distance between two points on the Earth. Now that you have the latitude and longitude values in decimals, you just face the equation for the nautical miles between Timbuktu and Casablanca, noted underneath. Nautical Miles = ACOS [ (sin (Lat_place_1*PI ()/180)*sin (Lat_place_2*PI ()/180)+ cos (Lat_place_1*PI ()/180)*cos (Lat_place_2*PI ()/180)*

Here is a page with javascript examples for various spherical calculations. The very first one on the page should give you what you need.

http://www.movable-type.co.uk/scripts/latlong.html

Here is the Javascript code

```var R = 6371; // km
var dLat = (lat2-lat1).toRad();
var dLon = (lon2-lon1).toRad();
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.sin(dLon/2) * Math.sin(dLon/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
```

Where 'd' will hold the distance.

Calculate Distance Between Two Points Using Latitude and Longitude, In this post, we show the formula to calculate the shortest distance between two points using Latitude and Longitude. This calculation 'OUTPUT: Distance between the ' two points in Meters. ' '===== ' Calculate geodesic distance (in m) ' between two points specified by ' latitude/longitude (in numeric ' [decimal] degrees) ' using Vincenty inverse formula ' for ellipsoids '===== ' Code has been ported by lost_species ' from www.aliencoffee.co.uk to VBA ' from javascript

Latitude/Longitude Distance Calculator, This tool calculates the straight line distance between two pairs of latitude/​longitude points provide in decimal degrees. Additionally, this tool will convert your  select Distance from Place P1, Place P2, outer apply dbo.Get_Distance(P1.latitude, P1.longitude, P2.latitude, P2.longitude) Scalar functions also work but they are very inefficient when computing large amount of data.

Ask Dr. Math Archives: Latitude and Longitude, Distance Between Two Points on the Earth: My latitude and longitude are in the form Distance Calculation: If I have the co-ordinates of two places in Degrees  One of the most common ways to calculate distances using latitude and longitude is the haversine formula, which is used to measure distances on a sphere. This method uses spherical triangles and measures the sides and angles of each to calculate the distance between points.

Calculate Distance by Latitude and Longitude using JavaScript , This routine calculates the distance between two points (given the ::: //::: latitude/​longitude of those points). It is being used to calculate ::: //::: the distance  Here my code I used below code to calculate the distance between two location using their latitude and longitude. It is giving wrong distance. sometimes getting right and sometimes getting irrelevant distance. We are getting lat1 and lng1 from database.