Is there a way to calculate angle between two Latitude/Longitude points?
What I am trying to achieve is to know where the user is heading. For example, user is head
Based on Nayanesh Gupte's answer, here is a Python implementation of how to calculate the angle between two points defined by their latitudes and longitudes if anyone needs it:
def angleFromCoordinate(lat1, long1, lat2, long2):
dLon = (long2 - long1)
y = math.sin(dLon) * math.cos(lat2)
x = math.cos(lat1) * math.sin(lat2) - math.sin(lat1) * math.cos(lat2) * math.cos(dLon)
brng = math.atan2(y, x)
brng = math.degrees(brng)
brng = (brng + 360) % 360
brng = 360 - brng # count degrees clockwise - remove to make counter-clockwise
return brng
Where an angle of 0 degrees indicates a northward heading.
Considering Nayanesh Gupte's answer and its comments. I've changed some part of the code and wrote it in PHP
.
Here is the function:
function angleFromCoordinate($lat1, $long1, $lat2, $long2) {
$lat1 = deg2rad($lat1);
$lat2 = deg2rad($lat2);
$long1 = deg2rad($long1);
$long2 = deg2rad($long2);
$dLon = $long2 - $long1;
$y = sin($dLon) * cos($lat2);
$x = cos($lat1) * sin($lat2) - sin($lat1) * cos($lat2) * cos($dLon);
$brng = atan2($y, $x);
$brng = $brng * 180 / pi();
$brng = fmod($brng + 360, 360);
return $brng;
}
You just can use the google maps computeHeading:
var point1 = new google.maps.LatLng(lat1, lng1);
var point2 = new google.maps.LatLng(lat2, lng2);
var heading = google.maps.geometry.spherical.computeHeading(point1,point2);
If your are using google maps(Android), there is an easy way - Use SphericalUtil
double angle = SphericalUtil.computeHeading(fromLatLng, toLatLng);
Consider we have 2 points and its lat and lng Then create its Latlng object
LatLng latlng = new LatLng(latValue, lngValue);
After getting Latlng of 2 points, use sperical util to get angle
//import com.google.maps.android.SphericalUtil;
double sphericalValue = SphericalUtil.computeHeading(latLng1, latLng2);
SpericalValue is the angle. Consider you have a car icon and turn it accordingly to the direction its going. Here its from latLng1 to latLng2 then
Bitmap vehiclePin = rotateIconBitmap(sphericalValue);
mMap.addMarker(new MarkerOptions().anchor(0.5f, 0.5f).position(latLng2))
.setIcon(BitmapDescriptorFactory.fromBitmap(vehiclePin));
use the method below to rotate
Bitmap rotateIconBitmap(double angle) {
Bitmap source = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_vehicle_say_car);
Matrix matrix = new Matrix();
matrix.postRotate((float) angle);
return Bitmap.createBitmap(source, 0, 0,
source.getWidth(), source.getHeight(), matrix, true);
}
Easy way to achieve uber like rotated icons
Note:- You may have to add an offset of say 90 degree if the marker icon is not pointed to zero degree
The android sphericalutil is open source, refer it if you are using java, you can make use of it.
https://github.com/googlemaps/android-maps-utils/blob/master/library/src/com/google/maps/android/SphericalUtil.java
I think you want the calculations for the Great Circle bearing.
If you require an accurate method on an ellipsoid of revolution (i.e. WGS 84), the algorithms get really heavy. You may benefit from GeographicLib, which has been implemented in C/C++, Java, JavaScript, Python, Matlab/Octave, and others.
For the question, there is either a geodesic or a rhumb line between the first and second point.
A geodesic is the shortest path between two points on a curved surface. It is the most common interpretation of where a "user is heading" (from the question), since it is the shortest and most direct. An inverse geodesic calculation can be solved using GeodSolve. You can also use the online interface. This tool has the input/output:
lat1 lon1 lat2 lon2 → azi1 azi2 s12
Where lat1 lon1 is the coordinate pair for the first point, and lat2 lon2 is the coordinate pair for the second point. All units are in degrees (not radians). The result, azi1 or α1, is the azimuth (a.k.a. bearing) from the start point, given in degrees clockwise from north. The second azimuth is at the second point, because the angle between the two points along a geodesic is not constant. And s12 is the distance between the two points, in metres, with an accuracy of 15 nm.
A rhumb line connects two coordinate points with a constant azimuth (or bearing). An inverse rhumb line calculation can be solved using RhumbSolve. You can also use the online interface. This tool has the input/output:
lat1 lon1 lat2 lon2 → azi12 s12
These parameters are the same as GeodSolve, except that azi12 is a constant angle between the points.