Java Distance Calculate distanceBetween2GeoPositionsInMeters(double latA, double lngA, double latB, double lngB)

Here you can find the source of distanceBetween2GeoPositionsInMeters(double latA, double lngA, double latB, double lngB)

Description

Calculate the distance in meters between two geographical coordinates.

License

Apache License

Parameter

Parameter Description
latA The latitude of point A.
lngA The longitude of point A.
latB The latitude of point B.
lngB The longitude of point B.

Return

The distance in meters.

Declaration

public static double distanceBetween2GeoPositionsInMeters(double latA, double lngA, double latB, double lngB) 

Method Source Code

//package com.java2s;
/*/*  w w  w  . j a  v a 2 s .c om*/
 * Copyright (c) 2016 Martin Pfeffer
 *
 * 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 {
    /**
     * Calculate the distance in meters between two geographical coordinates.
     *
     * @param latA The latitude of point A.
     * @param lngA The longitude of point A.
     * @param latB The latitude of point B.
     * @param lngB The longitude of point B.
     * @return The distance in meters.
     */
    public static double distanceBetween2GeoPositionsInMeters(double latA, double lngA, double latB, double lngB) {
        double earthRadius = 6371;
        double dLat = Math.toRadians(latB - latA);
        double dLng = Math.toRadians(lngB - lngA);
        double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(Math.toRadians(latA))
                * Math.cos(Math.toRadians(latB)) * Math.sin(dLng / 2) * Math.sin(dLng / 2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        return (int) (earthRadius * c * 1000);
    }
}

Related

  1. distanceBetween(double lon, double lat, double otherLon, double otherLat)
  2. distanceBetween(double startLatitude, double startLongitude, double endLatitude, double endLongitude)
  3. distanceBetween(double x1, double y1, double x2, double y2)
  4. distanceBetween(double x1, double y1, double x2, double y2)
  5. distanceBetween(double xpos1, double ypos1, double xpos2, double ypos2)
  6. distanceBetween2Points(float p1[], float p2[])
  7. distanceBetween2Points(float vectorX0, float vectorY0, float vectorXP, float vectorYP)
  8. distanceBetweenPoints(double ax, double ay, double bx, double by)
  9. DistanceBetweenPoints(double x1, double y1, double x2, double y2)