Calculate the spherical distance between two points using the Spherical law of Cosines formula. - Android Map

Android examples for Map:Location Distance

Description

Calculate the spherical distance between two points using the Spherical law of Cosines formula.

Demo Code

/*******************************************************************************
 * Copyright (c) 2011 MadRobot./*from  ww  w  .ja  va  2  s .co m*/
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *  Elton Kent - initial API and implementation
 ******************************************************************************/
//package com.java2s;

public class Main {
    /**
     * Calculate the spherical distance between two points using the Spherical law of Cosines formula.
     * <p>
     * Its a more accurate than the haversine distance and gives an accuracy of upto 1 metre.
     * </p>
     * 
     * @param lon1
     * @param lat1
     * @param lon2
     * @param lat2
     * @return Distance in Km
     */
    public static double lawOfCosineDistance(double lon1, double lat1,
            double lon2, double lat2) {
        return Math.acos(Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1)
                * Math.cos(lat2) * Math.cos(lon2 - lon1)) * 6371;
    }
}

Related Tutorials