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