Here you can find the source of distHaversineRAD(double lat1, double lon1, double lat2, double lon2)
Parameter | Description |
---|---|
lat1 | The y coordinate of the first point, in radians |
lon1 | The x coordinate of the first point, in radians |
lat2 | The y coordinate of the second point, in radians |
lon2 | The x coordinate of the second point, in radians |
public static double distHaversineRAD(double lat1, double lon1, double lat2, double lon2)
//package com.java2s; /*/*from w w w. j a v a2 s . c o m*/ * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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 { /** * * @param lat1 The y coordinate of the first point, in radians * @param lon1 The x coordinate of the first point, in radians * @param lat2 The y coordinate of the second point, in radians * @param lon2 The x coordinate of the second point, in radians * @return The distance between the two points, as determined by the Haversine formula, in radians. */ public static double distHaversineRAD(double lat1, double lon1, double lat2, double lon2) { //TODO investigate slightly different formula using asin() and min() http://www.movable-type.co.uk/scripts/gis-faq-5.1.html // Check for same position if (lat1 == lat2 && lon1 == lon2) return 0.0; double hsinX = Math.sin((lon1 - lon2) * 0.5); double hsinY = Math.sin((lat1 - lat2) * 0.5); double h = hsinY * hsinY + (Math.cos(lat1) * Math.cos(lat2) * hsinX * hsinX); return 2 * Math.atan2(Math.sqrt(h), Math.sqrt(1 - h)); } }