Here you can find the source of computeDistanceUsingRadians(double lat1Radians, double lng1Radians, double lat2Radians, double lng2Radians)
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 |
public static double computeDistanceUsingRadians(double lat1Radians, double lng1Radians, double lat2Radians, double lng2Radians)
//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); } }