/** * See * http://gis.stackexchange.com/questions/29239/calculate-bearing-between-two-decimal-gps-coordinates * * @param start * @param end * @return */ public static double getBearing(Location start, Location end) { double startLong = toRadians(start.getLongitude()); double startLat = toRadians(start.getLatitude()); double endLong = toRadians(end.getLongitude()); double endLat = toRadians(end.getLatitude()); double dLong = endLong - startLong; double dPhi = Math.log(tan(endLat / 2.0 + Math.PI / 4.0) / tan(startLat / 2.0 + Math.PI / 4.0)); if (Math.abs(dLong) > Math.PI) { if (dLong > 0.0) dLong = -(2.0 * Math.PI - dLong); else dLong = (2.0 * Math.PI + dLong); } return (toDegrees(Math.atan2(dLong, dPhi)) + 360.0) % 360.0; }
private static Location getMidpoint(Location start, Location end) { double startLat = toRadians(start.getLatitude()); double startLong = toRadians(start.getLongitude()); double endLat = toRadians(end.getLatitude()); double deltaLong = toRadians(end.getLongitude() - start.getLongitude()); double Bx = cos(endLat) * cos(deltaLong); double By = cos(endLat) * sin(deltaLong); double midLat = Math.atan2( sin(startLat) + sin(endLat), Math.sqrt((cos(startLat) + Bx) * (cos(startLat) + Bx) + By * By)); double midLong = startLong + Math.atan2(By, cos(startLat) + Bx); midLong = (midLong + 3 * Math.PI) % (2 * Math.PI) - Math.PI; // normalize to -180..+180° return new Location(toDegrees(midLat), toDegrees(midLong)); }
@Override public int compare(Location o1, Location o2) { if (Double.compare(o1.getLongitude(), o2.getLongitude()) == 0) return reverseDirection * Double.compare(o1.getLatitude(), o2.getLatitude()); return reverseDirection * Double.compare(o1.getLongitude(), o2.getLongitude()); }
public LocationComparator(Location start, Location end) { if (Double.compare(start.getLongitude(), end.getLongitude()) == 0) reverseDirection = -1 * Double.compare(start.getLatitude(), end.getLatitude()); reverseDirection = -1 * Double.compare(start.getLongitude(), end.getLongitude()); }