Is there any way to fetch the distance between two locations in Flutter?
Asked
Active
Viewed 4,854 times
0
-
https://stackoverflow.com/questions/54138750/total-distance-calculation-from-latlng-list – Roger Gusmao Oct 12 '20 at 17:48
3 Answers
5
You can find the distance by the HaverSine formula, implemented in dart as:
import'dart:math' as Math;
void main()=>print(getDistanceFromLatLonInKm(73.4545,73.4545,83.5454,83.5454));
double getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) {
var R = 6371; // Radius of the earth in km
var dLat = deg2rad(lat2-lat1); // deg2rad below
var dLon = deg2rad(lon2-lon1);
var a =
Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2)
;
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c; // Distance in km
return d;
}
double deg2rad(deg) {
return deg * (Math.pi/180);
}
Output:
1139.9231530436646
Source in Javscript and credits to Chuck.

cmd_prompter
- 1,566
- 10
- 16
1
You can use geolocator plugin if you are looking for the shortest distance between two locations aka LatLng.
double distanceInMeters =
await Geolocator().distanceBetween(52.2165157, 6.9437819, 52.3546274, 4.8285838);

CopsOnRoad
- 237,138
- 77
- 654
- 440
-
I alerady used this package but now I used this package flutter_background_geolocation I want to know is there is any methods do this job due to I wont use 1 package instead 2 – Mahmoud Niypoo Dec 21 '19 at 10:33
-
Sorry there is no way of doing it using `flutter_background_geolocation` plugin. – CopsOnRoad Dec 21 '19 at 14:21
1
I've used the vector_math
for converting degree to radians and also the geolocator
for getting the current user latitude and longitude if in case searching from the current location also there is a method where in you can calculate the distance between two locations directly as Geolocator.distanceBetween(startLatitude, startLongitude, endLatitude, endLongitude).
import 'dart:math' show sin, cos, sqrt, atan2;
import 'package:vector_math/vector_math.dart';
import 'package:geolocator/geolocator.dart';
Position _currentPosition;
double earthRadius = 6371000;
//Using pLat and pLng as dummy location
double pLat = 22.8965265; double pLng = 76.2545445;
//Use Geolocator to find the current location(latitude & longitude)
getUserLocation() async {
_currentPosition = await GeoLocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
}
//Calculating the distance between two points without Geolocator plugin
getDistance(){
var dLat = radians(pLat - _currentPosition.latitude);
var dLng = radians(pLng - _currentPosition.longitude);
var a = sin(dLat/2) * sin(dLat/2) + cos(radians(_currentPosition.latitude))
* cos(radians(pLat)) * sin(dLng/2) * sin(dLng/2);
var c = 2 * atan2(sqrt(a), sqrt(1-a));
var d = earthRadius * c;
print(d); //d is the distance in meters
}
//Calculating the distance between two points with Geolocator plugin
getDistance(){
final double distance = Geolocator.distanceBetween(pLat, pLng,
_currentPosition.latitude, _currentPosition.longitude);
print(distance); //distance in meters
}

Niroop Nife
- 356
- 1
- 5
- 18