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

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

Description

distance

License

Open Source License

Declaration

public static double distance(double lat1, double lon1, double lat2, double lon2) 

Method Source Code

//package com.java2s;
/*/*from   w ww .j  ava 2s  .  co  m*/
 * Mosaic - Location Based Messaging
 * Copyright (C) 2013 Bryan Emmanuel
 * 
 * This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *  
 *  Bryan Emmanuel piusvelte@gmail.com
 */

public class Main {
    public static final int EARTH_RADIUS = 6378135;

    public static double distance(double lat1, double lon1, double lat2, double lon2) {
        double p1lat = Math.toRadians(lat1);
        double p1lon = Math.toRadians(lon1);
        double p2lat = Math.toRadians(lat2);
        double p2lon = Math.toRadians(lon2);
        return EARTH_RADIUS * Math.acos(makeDoubleInRange(
                Math.sin(p1lat) * Math.sin(p2lat) + Math.cos(p1lat) * Math.cos(p2lat) * Math.cos(p2lon - p1lon)));
    }

    public static double makeDoubleInRange(double d) {
        if (d > 1)
            return 1;
        else if (d < -1)
            return -1;
        return d;
    }
}

Related

  1. dist2Degrees(double dist, double radius)
  2. dist2Radians(double dist, double radius)
  3. distAlongRaySquared(double px, double py, double qx, double qy, double rx, double ry)
  4. distance(double lat1, double lng1, double lat2, double lng2)
  5. distance(double lat1, double lng1, double lat2, double lng2)
  6. distance(double lat1, double lon1, double lat2, double lon2)
  7. distance(double lat1, double lon1, double lat2, double lon2)
  8. distance(double lat1, double lon1, double lat2, double lon2)
  9. distance(double latA, double logA, double latB, double logB)