Java Distance Calculate computeDistanceUsingRadians(double lat1Radians, double lng1Radians, double lat2Radians, double lng2Radians)

Here you can find the source of computeDistanceUsingRadians(double lat1Radians, double lng1Radians, double lat2Radians, double lng2Radians)

Description

Returns the distance in meters between ( lat1 , lng1 ) and ( lat2 , lng2 ).

License

Apache License

Parameter

Parameter Description
lat1Radians Latitude of first point, in radians
lng1Radians Longitude of first point, in radians
lat2Radians Latitude of second point, in radians
lng2Radians Longitude of second point, in radians

Declaration

public static double computeDistanceUsingRadians(double lat1Radians, double lng1Radians, double lat2Radians,
        double lng2Radians) 

Method Source Code

//package com.java2s;
/*// w w  w.j  a v  a2 s. c om
 * Copyright (C) 2010 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

public class Main {
    /** Earth radius in meters. */
    public static final double EARTH_RADIUS = 6371009;

    /**
     * Returns the distance in meters between ({@code lat1}, {@code lng1}) and (
     * {@code lat2}, {@code lng2}).
     * <p>
     * Calculation is done by the Haversine Formula.
     * 
     * @param lat1Radians Latitude of first point, in radians
     * @param lng1Radians Longitude of first point, in radians
     * @param lat2Radians Latitude of second point, in radians
     * @param lng2Radians Longitude of second point, in radians
     * 
     * @see <a href="http://en.wikipedia.org/wiki/Haversine_formula"
     *      target="_parent">Haversine formula</a>
     */
    public static double computeDistanceUsingRadians(double lat1Radians, double lng1Radians, double lat2Radians,
            double lng2Radians) {
        final double dLat = lat2Radians - lat1Radians;
        final double dLng = lng2Radians - lng1Radians;
        final double a = haversin(dLat) + Math.cos(lat1Radians) * Math.cos(lat2Radians) * haversin(dLng);
        return EARTH_RADIUS * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    }

    /**
     * Returns the trigonometric haversine of an angle.
     * <p>
     * 
     * @param x Angle, in radians
     * @return <pre>
     * haversin(x) = (1-cos(x))/2 = sin^2(x/2)
     * </pre>
     * 
     * @see <a href="http://en.wikipedia.org/wiki/Versine"
     *      target="_parent">Versine function</a>
     */
    private static double haversin(final double x) {
        return Math.pow(Math.sin(x / 2), 2);
    }
}

Related

  1. computeDistanceAndBearingFast(double lat1, double lon1, double lat2, double lon2, float[] results)
  2. computeDistanceImpl(final String firstStringInAlgorithm, final String secondStringInAlgorithm)
  3. computeDistanceInMiles(Double lat1, Double lon1, Double lat2, Double lon2)
  4. computeDistanceMap(int[][] image)
  5. computeDistanceSqr(double t, double b1, double b2, double b3, double b4, double b5, double b6)
  6. dist(double lat1, double long1, double lat2, double long2)
  7. dist(double x1, double y1, double z1, double x2, double y2, double z2)
  8. dist(final double x0, final double y0, final double x1, final double y1)
  9. dist(float x1, float y1, float x2, float y2)