forked from liferay/liferay-portal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDistance.java
49 lines (39 loc) · 1.23 KB
/
Distance.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package com.liferay.util;
/**
* @author Brian Wing Shun Chan
*/
public class Distance {
public static double calculate(
double lat1, double lon1, double lat2, double lon2) {
// Convert from radians to degrees
lat1 = (Math.PI * lat1) / 180;
lon1 = (Math.PI * lon1) / 180;
lat2 = (Math.PI * lat2) / 180;
lon2 = (Math.PI * lon2) / 180;
double miles =
3963.4 *
Math.acos(
(Math.sin(lat1) * Math.sin(lat2)) +
(Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon1 - lon2)));
return miles;
}
public static double kmToMiles(double km) {
return km * 0.621;
}
public static double milesToKm(double miles) {
return miles / 0.621;
}
}