001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.util;
016    
017    /**
018     * @author Brian Wing Shun Chan
019     */
020    public class Distance {
021    
022            public static double calculate(double lat1, double lon1,
023                                                                       double lat2, double lon2) {
024    
025                    // Convert from radians to degrees
026    
027                    lat1 = (Math.PI * lat1) / 180;
028                    lon1 = (Math.PI * lon1) / 180;
029                    lat2 = (Math.PI * lat2) / 180;
030                    lon2 = (Math.PI * lon2) / 180;
031    
032                    double miles =
033                            3963.4 *
034                            Math.acos(
035                                    (Math.sin(lat1) * Math.sin(lat2)) +
036                                    (Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon1 - lon2)));
037    
038                    return miles;
039            }
040    
041            public static double kmToMiles(double km) {
042                    return km * 0.621;
043            }
044    
045            public static double milesToKm(double miles) {
046                    return miles / 0.621;
047            }
048    
049    }