Here you can find the source of distanceBetween(double lon, double lat, double otherLon, double otherLat)
Parameter | Description |
---|---|
lon | longitude, in degrees, of the first data location |
lat | latitude, in degrees, of the first data location |
otherlon | longitude, in degrees, of the other data location |
otherlat | latitude, in degrees, of the other data location |
public static double distanceBetween(double lon, double lat, double otherLon, double otherLat)
//package com.java2s; //License from project: Open Source License public class Main { /** Authalic radius, in kilometers, of Earth */ public static final double EARTH_AUTHALIC_RADIUS = 6371.007; /**//from ww w . j a v a 2 s . c om * Returns the distance between two locations. Uses the haversine formula, * and {@link DashboardUtils#EARTH_AUTHALIC_RADIUS} for the radius of a * spherical Earth, to compute the great circle distance from the * longitudes and latitudes. * * @param lon * longitude, in degrees, of the first data location * @param lat * latitude, in degrees, of the first data location * @param otherlon * longitude, in degrees, of the other data location * @param otherlat * latitude, in degrees, of the other data location * @return * the location-time distance between this location-time point * and other in kilometers */ public static double distanceBetween(double lon, double lat, double otherLon, double otherLat) { // Convert longitude and latitude degrees to radians double lat1 = lat * Math.PI / 180.0; double lat2 = otherLat * Math.PI / 180.0; double lon1 = lon * Math.PI / 180.0; double lon2 = otherLon * Math.PI / 180.0; /* * Use the haversine formula to compute the great circle distance, * in radians, between the two (longitude, latitude) points. */ double dellat = Math.sin(0.5 * (lat2 - lat1)); dellat *= dellat; double dellon = Math.sin(0.5 * (lon2 - lon1)); dellon *= dellon * Math.cos(lat1) * Math.cos(lat2); double distance = 2.0 * Math.asin(Math.sqrt(dellon + dellat)); // Convert the great circle distance from radians to kilometers distance *= EARTH_AUTHALIC_RADIUS; return distance; } }