Java tutorial
//package com.java2s; import android.location.Location; public class Main { /** * Computes the bearing in radians between two points on Earth. * * @param p1 First point * @param p2 Second point * @return Bearing between the two points in radians. A value of 0 means due * north. */ public static double bearingRad(Location p1, Location p2) { double lat1 = p1.getLatitude(); double lon1 = p1.getLongitude(); double lat2 = p2.getLatitude(); double lon2 = p2.getLongitude(); return bearingRad(lat1, lon1, lat2, lon2); } /** * Computes the bearing in radians between two points on Earth. * * @param lat1 Latitude of the first point * @param lon1 Longitude of the first point * @param lat2 Latitude of the second point * @param lon2 Longitude of the second point * @return Bearing between the two points in radians. A value of 0 means due * north. */ public static double bearingRad(double lat1, double lon1, double lat2, double lon2) { double lat1Rad = Math.toRadians(lat1); double lat2Rad = Math.toRadians(lat2); double deltaLonRad = Math.toRadians(lon2 - lon1); double y = Math.sin(deltaLonRad) * Math.cos(lat2Rad); double x = Math.cos(lat1Rad) * Math.sin(lat2Rad) - Math.sin(lat1Rad) * Math.cos(lat2Rad) * Math.cos(deltaLonRad); return Math.atan2(y, x); } }