interpolate static method

ILatLng interpolate(
  1. ILatLng from,
  2. ILatLng to,
  3. num fraction
)

Returns the LatLng which lies the given fraction of the way between the origin LatLng and the destination LatLng. @param from The LatLng from which to start. @param to The LatLng toward which to travel. @param fraction A fraction of the distance to travel. @return The interpolated LatLng.

Implementation

static ILatLng interpolate(ILatLng from, ILatLng to, num fraction) {
  if (from.isEmpty) return to;

  final fromLat = from.latitude.radians;
  final fromLng = from.longitude.radians;
  final toLat = to.latitude.radians;
  final toLng = to.longitude.radians;
  final cosFromLat = math.cos(fromLat);
  final cosToLat = math.cos(toLat);

  // Computes Spherical interpolation coefficients.
  final angle = computeAngleBetween(from, to);
  final sinAngle = math.sin(angle);

  if (sinAngle < 1E-6) {
    return LatLngInfo(
      from.latitude + fraction * (to.latitude - from.latitude),
      from.longitude + fraction * (to.longitude - from.longitude),
      from.markerId,
    );
  }

  final a = math.sin((1 - fraction) * angle) / sinAngle;
  final b = math.sin(fraction * angle) / sinAngle;

  // Converts from polar to vector and interpolate.
  final x =
      a * cosFromLat * math.cos(fromLng) + b * cosToLat * math.cos(toLng);
  final y =
      a * cosFromLat * math.sin(fromLng) + b * cosToLat * math.sin(toLng);
  final z = a * math.sin(fromLat) + b * math.sin(toLat);

  // Converts interpolated vector back to polar.
  final lat = math.atan2(z, math.sqrt(x * x + y * y));
  final lng = math.atan2(y, x);

  return LatLngInfo(
    lat.degrees,
    lng.degrees,
    from.markerId,
    ripple: from.ripple,
  );
}