destinationPoint method

  1. @override
Geographic destinationPoint({
  1. required double distance,
  2. required double bearing,
  3. double radius = 6371000.0,
})

Returns the destination point having travelled along a rhumb line from the current position the given distance on the given bearing.

Parameters:

  • distance: Distance travelled (same units as radius, default: metres).
  • bearing: The bearing in degrees from north (0°..360°).
  • radius: The radius of earth (defaults to mean radius in metres).

Examples:

  const p1 = Geographic(lat: 51.127, lon: 1.338);

  // destination point (lat: 50.9642°N, lon: 001.8530°E)
  final p2 = p1.rhumb.
       destinationPoint(distance: 40300.0, bearing: 116.7);

Implementation

@override
Geographic destinationPoint({
  required double distance,
  required double bearing,
  double radius = 6371000.0,
}) {
  if (distance == 0.0) return position;

  final lat1 = position.lat.toRadians();
  final lon1 = position.lon.toRadians();
  final brng = bearing.toRadians();

  final dst = distance / radius; // angular distance in radians

  final dlat = dst * cos(brng);
  var lat2 = lat1 + dlat;

  // check for some daft bugger going past the pole, normalise latitude if so
  if (lat2.abs() > pi / 2.0) {
    lat2 = lat2 > 0.0 ? pi - lat2 : -pi - lat2;
  }

  final dlatProj =
      log(tan(lat2 / 2.0 + pi / 4.0) / tan(lat1 / 2.0 + pi / 4.0));
  final q = dlatProj.abs() > 10e-12 ? dlat / dlatProj : cos(lat1);

  final dlon = dst * sin(brng) / q;
  final lon2 = lon1 + dlon;

  return Geographic(lat: lat2.toDegrees(), lon: lon2.toDegrees());
}