77

Possible Duplicate:
Working with latitude/longitude values in Java

Duplicate:

I need to calculate the distance between two points given by two coordinates. The project I am working on is a Java-project, so Java-code will be great, but pseudo-code can also be given, then I can implement it myself :)

As you probably know, there are three ways to represent coordinates:

  • Degrees:Minutes:Seconds (49°30'00"N, 123°30'00"W)
  • Degrees:Decimal Minutes (49°30.0', -123°30.0'), (49d30.0m,-123d30.0')
  • Decimal Degrees (49.5000°,-123.5000°), generally with 4-6 decimal numbers.

It's the third way my coordinates are given in, so the code for this values will be preferred :)

Community
  • 1
  • 1
Espen Herseth Halvorsen
  • 6,175
  • 7
  • 36
  • 38
  • 2
    http://en.wikipedia.org/wiki/Haversine_formula – dotjoe May 08 '09 at 01:55
  • Just out of curiosity. As the earth isnt a perfect sphere, (equatorial radius of 6,378.137 km and a polar radius of 6,356.752 km.) How large would the error be? 3958.75 miles in the answer below isan approximation using the volumetric radius – KarlP May 08 '09 at 12:32
  • 2
    See the Vicenty formula -- http://www.movable-type.co.uk/scripts/latlong-vincenty.html -- if you care about the Earth not quite being a sphere – mob Nov 10 '09 at 23:22
  • I used the code from this site successfully: * [Longditude and latitude calculation](http://www.zipcodeworld.com/samples/distance.java.html) I had to make some calculations on the answers to make it into european meters, but that was pretty straight forward :) – Espen Herseth Halvorsen May 08 '09 at 01:48
  • Take a look at [GeoTools](http://geotools.codehaus.org/)' `org.geotools.referencing.GeodeticCalculator`. Set the starting and ending position then get call [`getOrthodromicDistance()`](http://docs.geotools.org/latest/javadocs/org/geotools/referencing/GeodeticCalculator.html#getOrthodromicDistance%28%29) which returns the orthodromic (great circle) distance. – Steve Kuo May 08 '09 at 03:45
  • Take a look at this latitude/longitude distance calculator. It has a link to relevant source code (including an explanation) and a link to the math behind the calculation. It's actually pretty interesting. – lc. May 08 '09 at 01:42
  • If you understand mathematical formulas then this page should be more than enough to calculate distance between two points in any programming language. [http://en.wikipedia.org/wiki/Geographical_distance](http://en.wikipedia.org/wiki/Geographical_distance) – jan May 08 '09 at 11:21
  • [This site](http://www.movable-type.co.uk/scripts/latlong.html) shows you the formula which is the part I assume you are having trouble with? You might want to specify if you want a straight line from point A to point B (through Earth) or if you want the distance as presented on the site. I'm sorry I don't exactly recall their names. However, the code seems relatively straightforward. – nevets1219 May 08 '09 at 01:43

3 Answers3

193

Based on another question on stackoverflow, I got this code.. This calculates the result in meters, not in miles :)

 public static float distFrom(float lat1, float lng1, float lat2, float lng2) {
    double earthRadius = 6371000; //meters
    double dLat = Math.toRadians(lat2-lat1);
    double dLng = Math.toRadians(lng2-lng1);
    double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
               Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
               Math.sin(dLng/2) * Math.sin(dLng/2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
    float dist = (float) (earthRadius * c);

    return dist;
    }
Community
  • 1
  • 1
Espen Herseth Halvorsen
  • 6,175
  • 7
  • 36
  • 38
27

You can use the Java Geodesy Library for GPS, it uses the Vincenty's formulae which takes account of the earths surface curvature.

Implementation goes like this:

import org.gavaghan.geodesy.*;

...

GeodeticCalculator geoCalc = new GeodeticCalculator();

Ellipsoid reference = Ellipsoid.WGS84;  

GlobalPosition pointA = new GlobalPosition(latitude, longitude, 0.0); // Point A

GlobalPosition userPos = new GlobalPosition(userLat, userLon, 0.0); // Point B

double distance = geoCalc.calculateGeodeticCurve(reference, userPos, pointA).getEllipsoidalDistance(); // Distance between Point A and Point B

The resulting distance is in meters.

HamZa
  • 14,671
  • 11
  • 54
  • 75
Oscar Salguero
  • 10,275
  • 5
  • 49
  • 48
4

In C++ it is done like this:

#define LOCAL_PI 3.1415926535897932385 

double ToRadians(double degrees) 
{
  double radians = degrees * LOCAL_PI / 180;
  return radians;
}

double DirectDistance(double lat1, double lng1, double lat2, double lng2) 
{
  double earthRadius = 3958.75;
  double dLat = ToRadians(lat2-lat1);
  double dLng = ToRadians(lng2-lng1);
  double a = sin(dLat/2) * sin(dLat/2) + 
             cos(ToRadians(lat1)) * cos(ToRadians(lat2)) * 
             sin(dLng/2) * sin(dLng/2);
  double c = 2 * atan2(sqrt(a), sqrt(1-a));
  double dist = earthRadius * c;
  double meterConversion = 1609.00;
  return dist * meterConversion;
}
user85421
  • 28,957
  • 10
  • 64
  • 87
jessn
  • 1
  • 2