rhumbDestination function
Returns the destination Point having travelled the given distance along a Rhumb line from the origin Point with the (varant) given bearing.
example:
var properties = { 'foo': 'bar' };
var point = Feature(geometry: Point(coordinates: Position.of([-75.343, 39.984])));
final destinationPoint = rhumbDestination(
feature.geometry!,
dist,
bearing,
unit: Unit.meters,
properties: feature.properties,
);
Implementation
Feature<Point> rhumbDestination(
Point origin,
num distance,
num bearing, {
Unit? unit = Unit.kilometers,
Map<String, dynamic>? properties,
}) {
unit ??= Unit.kilometers;
final wasNegativeDistance = distance < 0;
var distanceInMeters = convertLength(distance.abs(), unit, Unit.meters);
if (wasNegativeDistance) distanceInMeters = -(distanceInMeters.abs());
final coords = getCoord(origin);
final destination =
calculateRhumbDestination(coords, distanceInMeters, bearing);
// 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 - coords.lng) > 180
? -360
: (coords.lng - destination.lng) > 180
? 360
: 0;
return Feature<Point>(
geometry: Point(
coordinates:
Position(destination.lng + compensateLng, destination.lat)));
}