offset method

  1. @override
LatLng offset(
  1. LatLng from,
  2. double distanceInMeter,
  3. double bearing
)
override

Returns a destination point based on the given distance and bearing

Given a from (start) point, initial bearing, and distance, this will calculate the destination point and final bearing travelling along a (shortest distance) great circle arc.

final Haversine distance = const Haversine();

final num distanceInMeter = (earthRadius * math.PI / 4).round();

final p1 = new LatLng(0.0, 0.0);
final p2 = distance.offset(p1, distanceInMeter, 180);

Implementation

@override
LatLng offset(
    final LatLng from, final double distanceInMeter, final double bearing) {
  if (bearing < -180 || bearing > 180) {
    throw ArgumentError.value(
        bearing, 'bearing', 'Angle must be between -180 and 180 degrees');
  }

  final h = degToRadian(bearing.toDouble());

  final a = distanceInMeter / equatorRadius;

  final lat2 = math.asin(math.sin(from.latitudeInRad) * math.cos(a) +
      math.cos(from.latitudeInRad) * math.sin(a) * math.cos(h));

  final lng2 = from.longitudeInRad +
      math.atan2(math.sin(h) * math.sin(a) * math.cos(from.latitudeInRad),
          math.cos(a) - math.sin(from.latitudeInRad) * math.sin(lat2));

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