Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate distance in meters when you know longitude and latitude in java [duplicate]

Possible Duplicate:
Working with latitude/longitude values in Java

Duplicate:

  • Working with latitude/longitude values in Java
  • How do I calculate distance between two latitude longitude points?

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 :)

like image 464
Espen Herseth Halvorsen Avatar asked May 08 '09 01:05

Espen Herseth Halvorsen


People also ask

Can you calculate distance based on latitude and longitude?

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.

How do I calculate the distance between two points specified by latitude and longitude?

For this divide the values of longitude and latitude of both the points by 180/pi. The value of pi is 22/7. The value of 180/pi is approximately 57.29577951. If we want to calculate the distance between two places in miles, use the value 3, 963, which is the radius of Earth.


2 Answers

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;     } 
like image 100
Espen Herseth Halvorsen Avatar answered Sep 28 '22 05:09

Espen Herseth Halvorsen


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.

like image 26
Oscar Salguero Avatar answered Sep 28 '22 05:09

Oscar Salguero