Java Distance Calculate distanceFrom(double lat1, double long1, double lat2, double long2)

Here you can find the source of distanceFrom(double lat1, double long1, double lat2, double long2)

Description

Method calculating distance.

License

Open Source License

Parameter

Parameter Description
lat1 a parameter
long1 a parameter
lat2 a parameter
long2 a parameter

Return

distance between (lat1, long1) and (lat1, long2)

Declaration

public static double distanceFrom(double lat1, double long1, double lat2, double long2) 

Method Source Code

//package com.java2s;
/* /*ww  w.  ja  v a2s  .  c  o m*/
 * Licensed by AT&T under AT&T Public Source License Version 1.0.' 2012
 * 
 * TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION: http://developer.att.com/apsl-1.0
 * Copyright 2012 AT&T Intellectual Property. All rights reserved. http://pte.att.com/index.aspx
 * For more information contact: g15287@att.att-mail.com
 */

public class Main {
    /**
     * Method calculating distance.
     * 
     * @param lat1
     * @param long1
     * @param lat2
     * @param long2
     * @return distance between (lat1, long1) and (lat1, long2)
     */
    public static double distanceFrom(double lat1, double long1, double lat2, double long2) {
        double R = 6371; // km
        double rLat1 = Math.toRadians(lat1);
        double rLat2 = Math.toRadians(lat2);

        double dLat = rLat2 - rLat1;
        double dLon = Math.toRadians(long2 - long1);
        double a = Math.sin(dLat / 2.0) * Math.sin(dLat / 2.0)
                + Math.sin(dLon / 2.0) * Math.sin(dLon / 2.0) * Math.cos(rLat1) * Math.cos(rLat2);
        double c = 2.0 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        double d = R * c;

        return d;
    }
}

Related

  1. distanceByLBS(double lo1, double la1, double lo2, double la2)
  2. distanceCircle(int i0, int i1, int dir, int size)
  3. distanceCorrelation(final double[][] x, final double[][] y)
  4. distanceDamerauLevenshtein(CharSequence source, CharSequence target)
  5. distanceFast(double lat1, double lon1, double lat2, double lon2)
  6. DistanceFromLine(float cx, float cy, float ax, float ay, float bx, float by)
  7. distanceFromLineToPoint(double x1, double y1, double z1, double x2, double y2, double z2, double px, double py, double pz)
  8. distanceFromZero(int x, int y)
  9. distanceHaversine(Double lat1, Double lon1, Double lat2, Double lon2, String param)