Java Distance Calculate distanceInMeters(double lat1, double lon1, double lat2, double lon2)

Here you can find the source of distanceInMeters(double lat1, double lon1, double lat2, double lon2)

Description

This calculation is not the most accurate, but it takes less computation time.

License

Open Source License

Parameter

Parameter Description
lat1 latitude of point 1
lon1 longitude of point 1
lat2 latitude of point 2
lon2 longitude of point 2

Return

distance in meters

Declaration

static double distanceInMeters(double lat1, double lon1, double lat2, double lon2) 

Method Source Code

//package com.java2s;
/* MOD_V2.0/*from   ww w. ja v  a  2 s .  co m*/
 * Copyright (c) 2012 OpenDA Association
 * All rights reserved.
 *
 * This file is part of OpenDA.
 *
 * OpenDA is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * OpenDA is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with OpenDA.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    private static final double METERS_PER_DEGREE = 60. * 1852.27;

    /**
     * This calculation is not the most accurate, but it takes less computation time.
     *
     * @param lat1 latitude of point 1
     * @param lon1 longitude of point 1
     * @param lat2 latitude of point 2
     * @param lon2 longitude of point 2
     * @return distance in meters
     */
    static double distanceInMeters(double lat1, double lon1, double lat2, double lon2) {
        //this calculation is not the most accurate, but it takes less computation time.
        double dy = (lat2 - lat1) * METERS_PER_DEGREE;
        double phi = (lat2 + lat1) * 0.017453 / 2;
        double dx = (lon2 - lon1) * METERS_PER_DEGREE * Math.cos(phi);

        return Math.hypot(dx, dy);
    }
}

Related

  1. DistanceFromLine(float cx, float cy, float ax, float ay, float bx, float by)
  2. distanceFromLineToPoint(double x1, double y1, double z1, double x2, double y2, double z2, double px, double py, double pz)
  3. distanceFromZero(int x, int y)
  4. distanceHaversine(Double lat1, Double lon1, Double lat2, Double lon2, String param)
  5. distanceInf(double[] p1, double[] p2)
  6. distanceInMetersBetween(final double lat1, final double lng1, final double lat2, final double lng2)
  7. distanceInMilesBetween(float lat1, float lng1, float lat2, float lng2)
  8. distanceInMilesBetweenDouble(Float centerLat, Float centerLon, double lat, double lon)
  9. distanceInRadians(double latitude1, double longitude1, double latitude2, double longitude2)