# Java – Calculate distance in meters when you know longitude and latitude in java

distancejavalatitude-longitude

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 ðŸ™‚

#### Best Solution

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