Distance between two LatLng data types

My app requires me to display distance between two LatLng data types. The function returns the distance value in Km as a double data type. the following lines are used to type cast the result double to string to strip the value down to 2 decimal points.  You can change this value (2) to whatever you want.

 
  String inString = d.toStringAsFixed(2); // '2.35'
  double inDouble = double.parse(inString);
import 'dart:math' as math;

double geoDistance(
  LatLng latlon1,
  LatLng latlon2,
) {
  double lat1 = latlon1.latitude;
  double lon1 = latlon1.longitude;
  double lat2 = latlon2.latitude;
  double lon2 = latlon2.longitude;
  var p = 0.017453292519943295;
  var c = math.cos;
  var a = 0.5 -
      c((lat2 - lat1) * p) / 2 +
      c(lat1 * p) * c(lat2 * p) * (1 - c((lon2 - lon1) * p)) / 2;
  // Returns distance in Kilo-meters
  var d = (12742 * math.asin(math.sqrt(a)));
  String inString = d.toStringAsFixed(2); // '2.35'
  double inDouble = double.parse(inString);
  return inDouble;
}[2022-02-06 10_48_21-Firebaseflutter - FlutterFlow.png]  
3
7 replies