offset static method

ILatLong offset(
  1. ILatLong from,
  2. double distanceInMeter,
  3. double bearing
)

Calculates a destination point from a given starting point, distance, and bearing.

This uses the Haversine formula to calculate the destination point along a great circle arc.

Implementation

static ILatLong offset(final ILatLong from, final double distanceInMeter, double bearing) {
  assert(bearing >= 0 && bearing <= 360);
  // bearing: 0: north, 90: east, 180: south, 270: west
  //bearing = 90 - bearing;

  // 0: east, 90: north, +/- 180: west, -90: south
  final double h = bearing / 180 * pi;

  final double a = distanceInMeter / EQUATORIAL_RADIUS;

  final double lat2 = asin(sin(degToRadian(from.latitude)) * cos(a) + cos(degToRadian(from.latitude)) * sin(a) * cos(h));

  final double lng2 =
      degToRadian(from.longitude) + atan2(sin(h) * sin(a) * cos(degToRadian(from.latitude)), cos(a) - sin(degToRadian(from.latitude)) * sin(lat2));

  return LatLong(radianToDeg(lat2), radianToDeg(lng2));
}