calculateDistance method

Future<String> calculateDistance(
  1. LatLng firstLocation,
  2. LatLng secondLocation
)

Calculate distance between two location

Implementation

Future<String> calculateDistance(
    LatLng firstLocation, LatLng secondLocation) async {
  var p = 0.017453292519943295;
  var c = cos;
  var a = 0.5 -
      c((secondLocation.latitude - firstLocation.latitude) * p) / 2 +
      c(firstLocation.latitude * p) *
          c(secondLocation.latitude * p) *
          (1 - c((secondLocation.longitude - firstLocation.longitude) * p)) /
          2;
  var distance = 12742 * asin(sqrt(a));

  if (distance < 1) {
    return (double.parse(distance.toStringAsFixed(3)) * 1000)
            .toString()
            .split(".")[0] +
        " m";
  } else {
    return double.parse(distance.toStringAsFixed(2)).toString() + " km";
  }
}