interpolate static method
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 LatLng interpolate(LatLng from, LatLng to, num fraction) {
// http://en.wikipedia.org/wiki/Slerp
final fromLat = MathUtil.toRadians(from.latitude);
final fromLng = MathUtil.toRadians(from.longitude);
final toLat = MathUtil.toRadians(to.latitude);
final toLng = MathUtil.toRadians(to.longitude);
final cosFromLat = cos(fromLat);
final cosToLat = cos(toLat);
// Computes Spherical interpolation coefficients.
final angle = computeAngleBetween(from, to);
final sinAngle = sin(angle);
if (sinAngle < 1E-6) {
return LatLng(from.latitude + fraction * (to.latitude - from.latitude),
from.longitude + fraction * (to.longitude - from.longitude));
}
final a = sin((1 - fraction) * angle) / sinAngle;
final b = sin(fraction * angle) / sinAngle;
// Converts from polar to vector and interpolate.
final x = a * cosFromLat * cos(fromLng) + b * cosToLat * cos(toLng);
final y = a * cosFromLat * sin(fromLng) + b * cosToLat * sin(toLng);
final z = a * sin(fromLat) + b * sin(toLat);
// Converts interpolated vector back to polar.
final lat = atan2(z, sqrt(x * x + y * y));
final lng = atan2(y, x);
return LatLng(
MathUtil.toDegrees(lat).toDouble(), MathUtil.toDegrees(lng).toDouble());
}