Distance.java |
1 /** 2 * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved. 3 * 4 * The contents of this file are subject to the terms of the Liferay Enterprise 5 * Subscription License ("License"). You may not use this file except in 6 * compliance with the License. You can obtain a copy of the License by 7 * contacting Liferay, Inc. See the License for the specific language governing 8 * permissions and limitations under the License, including but not limited to 9 * distribution rights of the Software. 10 * 11 * 12 * 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 }