flutterdarttype-conversionlocationgoogle-geolocation

Is there a way to convert meters to kilometers in dart?


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?


Solution

  • 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?