-4

i am using geolocator: ^7.3.1 package They mentioned a function that calculates the distance between two geographical points , But the result comes in meters by default

How could this be done in Km

    getDistance(){
    double distanceInMeters = Geolocator.distanceBetween(52.2165157, 6.9437819, 52.3546274, 4.8285838);
    print(distanceInMeters); // result comes in meters by default which is 144851.67191816124 meters
  }

How can I get the result in kilometers, and in short number not like that long number in their example?

Zoe
  • 27,060
  • 21
  • 118
  • 148
Jack
  • 161
  • 1
  • 16

1 Answers1

1

Conversion

  double distanceInMeters = 144851.67191816124;
  double distanceInKiloMeters = distanceInMeters / 1000;
  double roundDistanceInKM =
      double.parse((distanceInKiloMeters).toStringAsFixed(2));

  print(distanceInMeters);
  print(distanceInKiloMeters);
  print(roundDistanceInKM);

Output

144851.67191816124
144.85167191816123
144.85

Is this helpful?

jmdlb
  • 127
  • 3
  • 13
  • yes !! thanks jmdlb , These calculations are only for the result that I referred to in my question, or for any result? i mean what if the distance was less than 1000 , is still will work as expected ? – Jack Jul 28 '21 at 05:24
  • @Moe Hmd If distanceInMeters = 144 then, { 144.0 | 0.144 | 0.14} – jmdlb Jul 28 '21 at 05:42