rhumbDistance function

num rhumbDistance(
  1. Point from,
  2. Point to, [
  3. Unit unit = Unit.kilometers
])

Calculates the distance along a rhumb line between two Point in degrees, radians, miles, or kilometers.

example:

var from = Feature(geometry: Point(coordinates: Position.of([-75.343, 39.984])));
var to = Feature(geometry: Point(coordinates: Position.of([-75.534, 39.123])));

var distance = turf.rhumbDistance(from, to, Unit.meters);

Implementation

num rhumbDistance(Point from, Point to, [Unit unit = Unit.kilometers]) {
  final origin = getCoord(from);
  final destination = getCoord(to);

  // compensate the crossing of the 180th meridian (https://macwright.org/2016/09/26/the-180th-meridian.html)
  // solution from https://github.com/mapbox/mapbox-gl-js/issues/3250#issuecomment-294887678
  final compensateLng = (destination.lng - origin.lng) > 180
      ? -360
      : (origin.lng - destination.lng) > 180
          ? 360
          : 0;

  final distanceInMeters = calculateRhumbDistance(
      origin, Position(destination.lng + compensateLng, destination.lat));
  final distance = convertLength(distanceInMeters, Unit.meters, unit);
  return distance;
}