destination method
Returns the Coordinate at a certain distance and bearing from the Coordinate.
distance
is in meters and bearing
is in degrees.
Uses the Haversine formula to calculate the new coordinate.
Example:
Coordinate(1, 2).destination(1000, 45); // Coordinate(3.0, 4.0)
Implementation
Coordinate destination(double distance, double bearing) {
final lat1 = latitude * (pi / 180);
final lon1 = longitude * (pi / 180);
final bearingRad = bearing * (pi / 180);
final d = distance / 6371000;
final lat2 =
asin(sin(lat1) * cos(d) + cos(lat1) * sin(d) * cos(bearingRad));
final lon2 = lon1 +
atan2(sin(bearingRad) * sin(d) * cos(lat1),
cos(d) - sin(lat1) * sin(lat2));
return Coordinate(lat2 * (180 / pi), lon2 * (180 / pi));
}