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

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

This question is related to java distance latitude-longitude

The answer is


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;
}

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.


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to distance

Function to calculate distance between two coordinates I want to calculate the distance between two points in Java How do I find the difference between two values without knowing which is larger? Should I use px or rem value units in my CSS? Calculating distance between two geographic locations Haversine Formula in Python (Bearing and Distance between two GPS points) Shortest distance between a point and a line segment Calculate distance in meters when you know longitude and latitude in java

Examples related to latitude-longitude

How to get a time zone from a location using latitude and longitude coordinates? What are the lengths of Location Coordinates, latitude and longitude? Using Address Instead Of Longitude And Latitude With Google Maps API Google Maps API - how to get latitude and longitude from Autocomplete without showing the map? Which data type for latitude and longitude? Calculating distance between two geographic locations How can I enter latitude and longitude in Google Maps? Calculate the center point of multiple latitude/longitude coordinate pairs How can I get city name from a latitude and longitude point? Calculating Distance between two Latitude and Longitude GeoCoordinates