Sometimes we have to calculate the distance between 2 locations when we have coordinates latitude and longitude for those locations. This article will discuss how you can calculate a straight line distance between 2 locations 2 programming languages, JAVA & Swift.

## 1. Code to Find Distance in JAVA

In JAVA we don’t have any builtin framework to calculate such GEO distance directly. But we can do it using mathematical formulas in the following way.

```
public static double distanceInKm(double lat1, double lon1, double lat2, double lon2) {
double theta = lon1 - lon2;
double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
dist = Math.acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
dist = dist * 1.609344;
return (dist);
}
private static double deg2rad(double deg) {
return (deg * Math.PI / 180.0);
}
private static double rad2deg(double rad) {
return (rad * 180.0 / Math.PI);
}
```

This function takes 4 parameters which represent 2 coordinates, latitude, and longitude for both locations respectively. It performs mathematical calculations by considering the circular Earth. This function can be used in Android as well. Here you can find a Utility Method to Fetch Current Location in Android.

## 2. Code to Find Distance in Swift

You can use the following function to get the distance between 2 locations in swift IOS by passing latitude and longitude for both locations.

```
import CoreLocation
...
public static func distanceInKm(_ lat1: Double, _ lon1: Double, _ lat2: Double, _ lon2: Double) -> Double {
let coordinate1 = CLLocation(latitude: lat1, longitude: lon1)
let coordinate2 = CLLocation(latitude: lat2, longitude: lon2)
let distanceInMeters: Double = coordinate1.distance(from: coordinate2)
return distanceInMeters / 1000;
}
```

As you can see In Swift it’s very easy to do as we don’t need to do any mathematical calculation. We can use Apple’s CoreLocation Framework and can call `distance(...)`

method available in `CLLocation`

class. This method returns the distance in meters which is again divided by 1000 to return you in Kilometers, as you can see in the above code. You can get the user’s current coordinates as explained in this article, Get Current Location in Swift IOS.

## Convert Distance from KM to MILES

Above, both methods return the distance in Kilometers if you want to convert it into miles you get in with the following code.

`distanceInMiles = distanceInKM * 0.62137`

## Limitations of the above methods

You might have got the idea till now that both the above methods return the perpendicular distance not the by road distance. Although this perpendicular and straight line distance is enough for most of the cases. But if you are looking for By road distance then you need to use Google Direction API.

Here you can find our Javascript article written on the topic, How to Calculate By Road Distance Using Google Direction API.

If you have any questions feel free to ask in the comments section below.

You can also visit our tutorials list for other helping articles.