5

How to calculate distance between two location in dart?

BIS Tech
  • 17,000
  • 12
  • 99
  • 148
  • 5
    Well, if that works and it is returning the distance in kilometers, then simply multiply by 1000 and you'll get the distance in meters. `return 1000 * 12742 * asin(sqrt(a));` – drogel Mar 03 '20 at 08:38

5 Answers5

17

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))

BIS Tech
  • 17,000
  • 12
  • 99
  • 148
  • I used your answer but i dont know why the result is not getting right. Like I used ```LatLng(19.975100, 73.772400) and LatLng(20.354740, 74.000488)``` this two which actual distance is hardly 1 km but by using this method i get ```16.036513885057946``` can you please me – Shubham Narkhede Apr 11 '22 at 16:51
11

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.

Amit Jangid
  • 2,741
  • 1
  • 22
  • 28
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.

jnnks
  • 866
  • 6
  • 17
1

If you need to check distance between only 2 coordinates, you should use Geolocator().distanceBetween, it is simple, but if you have a list of coordinates maybe you don't want use request for every item, in this case you can use this code to optimize performance:

double calculateDistance(LatLng start, LatLng end) {

  const double earthRadius = 6371.0; // Radius of the Earth in kilometers

  // Convert coordinates to radians
  final double lat1 = start.latitude * (pi / 180.0);
  final double lon1 = start.longitude * (pi / 180.0);
  final double lat2 = end.latitude * (pi / 180.0);
  final double lon2 = end.longitude * (pi / 180.0);

  // Calculate the differences between the coordinates
  final double dLat = lat2 - lat1;
  final double dLon = lon2 - lon1;

  // Apply the Haversine formula
  final double a = sin(dLat / 2) * sin(dLat / 2) +
      cos(lat1) * cos(lat2) * sin(dLon / 2) * sin(dLon / 2);
  final double c = 2 * atan2(sqrt(a), sqrt(1 - a));
  final double distance = earthRadius * c;

  return distance; // Distance in kilometers, add "*1000" to get meters
}
KiriLL Sabko
  • 113
  • 1
  • 5
0

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.

A. Sahin
  • 51
  • 3
  • 1
    This is a useful addition but should be a full answer and not a comment directed towards another answer. Consider expanding it into a full answer. Once you earn enough reputation you will be able to comment on or even edit others' answers. – Bracken Oct 04 '21 at 11:59