Reputation: 19504
How to calculate current location change by 10 meters? 10 meters is dynamically changed.
I tried, location
plugin on Flutter. but not working curretly.
location.changeSettings(distanceFilter: 10,interval: 1000);
//10 is meters, but location is updating every time.
How to calculate like this, I need to know how to calculate.(Because I need to calculate waiting time when travelling)
if(currentLocation > previousLocation) // currentLocation should be greater than 10 meters
currentLication = previousLocation + 10 meters
Upvotes: 0
Views: 1358
Reputation: 2943
I don't think location plugin provide such functionality at this time, But you can use onLocationChanged
callback event
location.onLocationChanged().listen((LocationData currentLocation) {
// Use current location
});
It will return you current location, than you have to calculate it by using Haversine formula, see more details here and here
a = sin²(Δφ/2) + cos φ1 ⋅ cos φ2 ⋅ sin²(Δλ/2)
c = 2 ⋅ atan2( √a, √(1−a) )
d = R ⋅ c
below dart code can help you to calculate difference and get next location:
import 'dart:math' show cos, sqrt, asin;
double calculateDistance(LatLng l1, LatLng l2) {
const p = 0.017453292519943295;
final a = 0.5 -
cos((l2.latitude - l1.latitude) * p) / 2 +
cos(l1.latitude * p) *
cos(l2.latitude * p) *
(1 - cos((l2.longitude - l1.longitude) * p)) /
2;
return 12742 * asin(sqrt(a));
}
Upvotes: 2