001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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(
023                    double lat1, double lon1, 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    }