Reputation: 1986
I am trying to track user's speed and other gps data using location package. Whenever user click on start trip button I am calling this method.
Future<void> _listenLocation(store) async {
print('listen 1');
location.changeSettings(
accuracy: LocationAccuracy.high, distanceFilter: 20, interval: 5000);
_locationSubscription =
location.onLocationChanged.handleError((dynamic err) {
setState(() {
_error = err.code;
});
_locationSubscription.cancel();
}).listen((LocationData currentLocation) {
print('listen 2');
setState(() {
print('listen 3');
_error = null;
_location = currentLocation;
print('_location');
print(_location.speed);
print(currentLocation.speed);
});
});
}
I am getting data in listen only when user is moving or coordinates are changing. How can I get data even when user is not moving ?
Upvotes: 3
Views: 2801
Reputation: 189
I have used Geolocator package for getting the speed of any vehicle.
StreamSubscription<Position> _positionStream;
double _speed = 0.0;
@override
void initState() {
_positionStream =
Geolocator.getPositionStream(desiredAccuracy: LocationAccuracy.high)
.listen((position) {
_onSpeedChange(position == null ? 0.0 : (position.speed * 18) / 5); //Converting position speed from m/s to km/hr
});
super.initState();
}
void _onSpeedChange(double newSpeed) {
setState(() {
_speed = newSpeed;
});
}
@override
void dispose() {
_positionStream.cancel();
super.dispose();
}
Then use the _speed
value wherever you need.
Hope this helps you to solve your problem!
Upvotes: 0
Reputation: 77354
How can I get data even when user is not moving ?
Just query it. The callback is for situations where you want to know when the data changed. Since you need the data now, whether it changes or not, just get it:
_location = await location.getLocation();
Upvotes: 0