Q: solution for calculate trip distance I am creating a trip distance calculation app for our drivers. Above way is working. but always calculate distance.
Why always return different LatLng(s) without moving mobile? (mobile on my desk)
Output
I am getting more than 10km result within 10 minutes without moving the device. Why is that?
double calculate(){
double cal = 0;

list.removeWhere((element) => element?.latitude == userLocation?.latitude || element?.longitude == userLocation?.longitude); //remove duplicate values
list.add(userLocation);
          
if (list.length > 1) {
   LocationModel firstLoc = list.elementAt(list.length - 2);
   LocationModel lastLoc = list.last;

   cal = _cp.calculateDistance(firstLoc.latitude, firstLoc.longitude, lastLoc.latitude, lastLoc.longitude);
}

 totalKm += cal;
 retun totalKm;
}


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

GpsService Method
I am using geolocator plugin for that. (FusedLocationProviderClient or if not available the LocationManager on Android and CLLocationManager on iOS).
  Future<LocationModel> getLocation() async {
    try {
      Position userLocation = await Geolocator.getCurrentPosition(forceAndroidLocationManager: false, desiredAccuracy: LocationAccuracy.high);
      _currentLocation = LocationModel.fromGeoLocatorData(userLocation);
    } on Exception catch (e) {
      print('Could not get location: $e');
    }
    return _currentLocation;
  }

Timer.periodic(Duration(seconds: 1), (Timer t) async {
      userLocation = await locationService.getLocation().then(getLocation);
    });


A: Location tracking is not very deterministic with mobile devices. Even if you stand still, as you poll position, it will change a little with every measurement. Also you should take accuracy of locations into account. Basically, a location you get, does not mean that the position is at exactly lat/long. It actually means, that with a confidence of 68%, the position is within a radius of accuracy meters around lat/long. [1].
Thus, you only should assume, that two locations are different, if the distance between them is bigger than the sum of both of its accuracies. And only if you've decided that a new location is different from the previous one, should you add the distance between them to the total distance.
Also, as one cannot move really far within a second (except perhaps in a race car or rocket) yoi could increase the time between measurements. This would also help in saving battery.
1: https://developer.android.com/reference/android/location/Location
