calculateDistance method

double calculateDistance(
  1. LatLng start,
  2. LatLng end
)

Property that defines the tour calculateDistance according to its current properties. Returns the distance between two given points.

Implementation

double calculateDistance(LatLng start, LatLng end) {
  const double earthRadius = 6371000; // Earth's radius in meters

  double dLat = _toRadians(end.latitude - start.latitude);
  double dLng = _toRadians(end.longitude - start.longitude);

  double a = sin(dLat / 2) * sin(dLat / 2) +
      cos(_toRadians(start.latitude)) *
          cos(_toRadians(end.latitude)) *
          sin(dLng / 2) *
          sin(dLng / 2);

  double c = 2 * atan2(sqrt(a), sqrt(1 - a));

  return earthRadius * c; // Distance in meters
}