offset method
Returns a destination point based on the given distance and bearing
Given a from
(start) point, initial bearing
, and distance,
this will calculate the destination point and
bearing travelling along a (shortest distance) great circle arc.
Haversine distance = Haversine(); double distanceInMeter = EARTH_RADIUS * math.PI / 4;
LatLng position = LatLng(0.0, 0.0); LatLng destination = distance.offset(position, distanceInMeter, 180);
Implementation
@override
LatLng offset(dynamic from, double? distanceInMeter, double bearing) {
from = LatLng.from(from);
Validate.inclusiveBetween(-180.0, 180.0, bearing,
"Bearing angle must be between -180 and 180 degrees");
double h = degToRadian(bearing);
double a = distanceInMeter! / EQUATOR_RADIUS;
double latitude = math.asin(math.sin(from.latitudeInRad) * math.cos(a) +
math.cos(from.latitudeInRad) * math.sin(a) * math.cos(h));
double longitude = from.longitudeInRad +
math.atan2(
math.sin(h) * math.sin(a) * math.cos(from.latitudeInRad),
math.cos(a) - math.sin(from.latitudeInRad) * math.sin(latitude),
);
return LatLng(radianToDeg(latitude), radianToDeg(longitude));
}