@Test public void testInterpolate() { expectLatLngApproxEquals(up, SphericalUtil.interpolate(up, up, 1 / 2.0)); expectLatLngApproxEquals(down, SphericalUtil.interpolate(down, down, 1 / 2.0)); expectLatLngApproxEquals(left, SphericalUtil.interpolate(left, left, 1 / 2.0)); expectLatLngApproxEquals(new LatLng(1, 0), SphericalUtil.interpolate(front, up, 1 / 90.0)); expectLatLngApproxEquals(new LatLng(1, 0), SphericalUtil.interpolate(up, front, 89 / 90.0)); expectLatLngApproxEquals( new LatLng(89, 0), SphericalUtil.interpolate(front, up, 89 / 90.0)); expectLatLngApproxEquals(new LatLng(89, 0), SphericalUtil.interpolate(up, front, 1 / 90.0)); expectLatLngApproxEquals( new LatLng(-1, 0), SphericalUtil.interpolate(front, down, 1 / 90.0)); expectLatLngApproxEquals( new LatLng(-1, 0), SphericalUtil.interpolate(down, front, 89 / 90.0)); expectLatLngApproxEquals( new LatLng(-89, 0), SphericalUtil.interpolate(front, down, 89 / 90.0)); expectLatLngApproxEquals( new LatLng(-89, 0), SphericalUtil.interpolate(down, front, 1 / 90.0)); expectLatLngApproxEquals( new LatLng(0, -91), SphericalUtil.interpolate(left, back, 1 / 90.0)); expectLatLngApproxEquals( new LatLng(0, -91), SphericalUtil.interpolate(back, left, 89 / 90.0)); expectLatLngApproxEquals( new LatLng(0, -179), SphericalUtil.interpolate(left, back, 89 / 90.0)); expectLatLngApproxEquals( new LatLng(0, -179), SphericalUtil.interpolate(back, left, 1 / 90.0)); expectLatLngApproxEquals( up, SphericalUtil.interpolate(new LatLng(45, 0), new LatLng(45, 180), 1 / 2.0)); expectLatLngApproxEquals( down, SphericalUtil.interpolate(new LatLng(-45, 0), new LatLng(-45, 180), 1 / 2.0)); expectLatLngApproxEquals(left, SphericalUtil.interpolate(left, back, 0)); expectLatLngApproxEquals(back, SphericalUtil.interpolate(left, back, 1.0)); expectLatLngApproxEquals( new LatLng(-37.756872, 175.325252), SphericalUtil.interpolate( new LatLng(-37.756891, 175.325262), new LatLng(-37.756853, 175.325242), 0.5)); }
public static LatLng interpolate(LatLng from, LatLng to, double fraction) { double fromLat = toRadians(from.latitude); double fromLng = toRadians(from.longitude); double toLat = toRadians(to.latitude); double toLng = toRadians(to.longitude); double cosFromLat = cos(fromLat); double cosToLat = cos(toLat); double angle = computeAngleBetween(from, to); double sinAngle = sin(angle); if (sinAngle < 1E-6) { return new LatLng( from.latitude + fraction * (to.latitude - from.latitude), from.longitude + fraction * (to.longitude - from.longitude)); } double a = sin((1 - fraction) * angle) / sinAngle; double b = sin(fraction * angle) / sinAngle; double x = a * cosFromLat * cos(fromLng) + b * cosToLat * cos(toLng); double y = a * cosFromLat * sin(fromLng) + b * cosToLat * sin(toLng); double z = a * sin(fromLat) + b * sin(toLat); double lat = atan2(z, sqrt(x * x + y * y)); double lng = atan2(y, x); return new LatLng(toDegrees(lat), toDegrees(lng)); }
SphericalUtil { public static LatLng interpolate(LatLng from, LatLng to, double fraction) { double fromLat = toRadians(from.latitude); double fromLng = toRadians(from.longitude); double toLat = toRadians(to.latitude); double toLng = toRadians(to.longitude); double cosFromLat = cos(fromLat); double cosToLat = cos(toLat); double angle = computeAngleBetween(from, to); double sinAngle = sin(angle); if (sinAngle < 1E-6) { return new LatLng( from.latitude + fraction * (to.latitude - from.latitude), from.longitude + fraction * (to.longitude - from.longitude)); } double a = sin((1 - fraction) * angle) / sinAngle; double b = sin(fraction * angle) / sinAngle; double x = a * cosFromLat * cos(fromLng) + b * cosToLat * cos(toLng); double y = a * cosFromLat * sin(fromLng) + b * cosToLat * sin(toLng); double z = a * sin(fromLat) + b * sin(toLat); double lat = atan2(z, sqrt(x * x + y * y)); double lng = atan2(y, x); return new LatLng(toDegrees(lat), toDegrees(lng)); } }
SphericalUtil { public static LatLng interpolate(LatLng from, LatLng to, double fraction) { double fromLat = toRadians(from.latitude); double fromLng = toRadians(from.longitude); double toLat = toRadians(to.latitude); double toLng = toRadians(to.longitude); double cosFromLat = cos(fromLat); double cosToLat = cos(toLat); double angle = computeAngleBetween(from, to); double sinAngle = sin(angle); if (sinAngle < 1E-6) { return new LatLng( from.latitude + fraction * (to.latitude - from.latitude), from.longitude + fraction * (to.longitude - from.longitude)); } double a = sin((1 - fraction) * angle) / sinAngle; double b = sin(fraction * angle) / sinAngle; double x = a * cosFromLat * cos(fromLng) + b * cosToLat * cos(toLng); double y = a * cosFromLat * sin(fromLng) + b * cosToLat * sin(toLng); double z = a * sin(fromLat) + b * sin(toLat); double lat = atan2(z, sqrt(x * x + y * y)); double lng = atan2(y, x); return new LatLng(toDegrees(lat), toDegrees(lng)); } private  SphericalUtil(); }
SphericalUtil { public static LatLng interpolate(LatLng from, LatLng to, double fraction) { double fromLat = toRadians(from.latitude); double fromLng = toRadians(from.longitude); double toLat = toRadians(to.latitude); double toLng = toRadians(to.longitude); double cosFromLat = cos(fromLat); double cosToLat = cos(toLat); double angle = computeAngleBetween(from, to); double sinAngle = sin(angle); if (sinAngle < 1E-6) { return new LatLng( from.latitude + fraction * (to.latitude - from.latitude), from.longitude + fraction * (to.longitude - from.longitude)); } double a = sin((1 - fraction) * angle) / sinAngle; double b = sin(fraction * angle) / sinAngle; double x = a * cosFromLat * cos(fromLng) + b * cosToLat * cos(toLng); double y = a * cosFromLat * sin(fromLng) + b * cosToLat * sin(toLng); double z = a * sin(fromLat) + b * sin(toLat); double lat = atan2(z, sqrt(x * x + y * y)); double lng = atan2(y, x); return new LatLng(toDegrees(lat), toDegrees(lng)); } private  SphericalUtil(); static double computeHeading(LatLng from, LatLng to); static LatLng computeOffset(LatLng from, double distance, double heading); static LatLng computeOffsetOrigin(LatLng to, double distance, double heading); static LatLng interpolate(LatLng from, LatLng to, double fraction); static double computeDistanceBetween(LatLng from, LatLng to); static double computeLength(List<LatLng> path); static double computeArea(List<LatLng> path); static double computeSignedArea(List<LatLng> path); }
SphericalUtil { public static LatLng interpolate(LatLng from, LatLng to, double fraction) { double fromLat = toRadians(from.latitude); double fromLng = toRadians(from.longitude); double toLat = toRadians(to.latitude); double toLng = toRadians(to.longitude); double cosFromLat = cos(fromLat); double cosToLat = cos(toLat); double angle = computeAngleBetween(from, to); double sinAngle = sin(angle); if (sinAngle < 1E-6) { return new LatLng( from.latitude + fraction * (to.latitude - from.latitude), from.longitude + fraction * (to.longitude - from.longitude)); } double a = sin((1 - fraction) * angle) / sinAngle; double b = sin(fraction * angle) / sinAngle; double x = a * cosFromLat * cos(fromLng) + b * cosToLat * cos(toLng); double y = a * cosFromLat * sin(fromLng) + b * cosToLat * sin(toLng); double z = a * sin(fromLat) + b * sin(toLat); double lat = atan2(z, sqrt(x * x + y * y)); double lng = atan2(y, x); return new LatLng(toDegrees(lat), toDegrees(lng)); } private  SphericalUtil(); static double computeHeading(LatLng from, LatLng to); static LatLng computeOffset(LatLng from, double distance, double heading); static LatLng computeOffsetOrigin(LatLng to, double distance, double heading); static LatLng interpolate(LatLng from, LatLng to, double fraction); static double computeDistanceBetween(LatLng from, LatLng to); static double computeLength(List<LatLng> path); static double computeArea(List<LatLng> path); static double computeSignedArea(List<LatLng> path);  }