'how to calculate distance between two location on Flutter?(result should be meters)

How to calculate distance between two location in dart?



Solution 1:[1]

Without using plugin

  double calculateDistance(lat1, lon1, lat2, lon2){
    var p = 0.017453292519943295;
    var c = cos;
    var a = 0.5 - c((lat2 - lat1) * p)/2 +
        c(lat1 * p) * c(lat2 * p) *
            (1 - c((lon2 - lon1) * p))/2;
    return 12742 * asin(sqrt(a));
  }

result is KM. if you want meter just multiply by 1000. return 1000 * 12742 * asin(sqrt(a))

Solution 2:[2]

You can use geolocator plugin to calculate to distances between two coordinates:

An Example:

var _distanceInMeters = await Geolocator().distanceBetween(
   _latitudeForCalculation,
   _longitudeForCalculation,
   _currentPosition.latitude,
   _currentPosition.longitude,
);

Check out GeoLocator Plugin for more info.

Solution 3:[3]

Check out latlong. It's a package dedicated to work with coordinates and features sophisticated calculations.

Unlike packages like geolocator it does not come with integrated location services and can be used alongside packages specialized for that.

Solution 4:[4]

You can use latlong2 which is the continuation of latlong. It has features like distance calculation and path drawing. So you can get distance as path distance as well as direct distance.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 BIS Tech
Solution 2
Solution 3 jnnks
Solution 4