flutter-geolocator
flutter-geolocator copied to clipboard
Problem getting user speed
💬 Questions and Help
I'm struggling to get the user speed.
double velocity = 0;
@override
void initState() {
_determinePosition(); //just for authorisations
Geolocator.getPositionStream(
locationSettings: const LocationSettings(
accuracy: LocationAccuracy.bestForNavigation,
//distanceFilter: 0,
)).listen((Position position) {
_onAccelerate(position);
});
super.initState();
}
void _onAccelerate(Position position) {
setState(() {
velocity = position.speed;
});
}
Future<Position> _determinePosition() async {
bool serviceEnabled;
LocationPermission permission;
serviceEnabled = await Geolocator.isLocationServiceEnabled();
if (!serviceEnabled) {
// not enabled
return Future.error('Location services are disabled.');
}
permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
if (permission == LocationPermission.denied) {
// denied
return Future.error('Location permissions are denied');
}
}
if (permission == LocationPermission.deniedForever) {
// Permissions are denied forever, handle appropriately.
return Future.error(
'Location permissions are permanently denied, we cannot request permissions.');
}
return await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.bestForNavigation);
}
-1 km/h is not fast...
Hi @SwiftyFlow,
On which platform (Android, iOS, macOS, web or Windows) are you experiencing this problem?
Both android and iOS
I managed to go at 4.50km/h!
Future<void> _getSpeed() async {
await Geolocator.requestPermission();
final LocationSettings settings = _createLocationSettings();
while (_running) {
await Future<void>.delayed(const Duration(milliseconds: 1000));
Geolocator.getPositionStream(locationSettings: settings).listen(
(Position position) {
print('Received position: $position');
if (position.speed.isNegative) return;
setState(() {
velocity = position.speed;
context.read<MaxSpeedProvider>().setCurrentSpeedTo(position.speed);
if (velocity > context.read<MaxSpeedProvider>().maxSpeed) {
context.read<MaxSpeedProvider>().setMaxSpeedTo(velocity);
}
});
},
);
}
}
Still not happy with the result as it doesn't reflect my real speed. I have tried a stream builder but I cannot yield the result because of async issues. I'm stuck...
Hi @SwiftyFlow, the geolocator plugin calls the native APIs to get position information. We do not have control over the accuracy of the readings.
We do expose the accuracy values, however. You can get them from a Position
by calling position.speedAccuracy
. Documentation about what this accuracy means can be found here for Android and here for iOS. These values can tell you how accurate the speed readings are.
Without additional information, we are unfortunately not able to resolve this issue. Therefore, we reluctantly closed this issue for now. If you run into this issue later, feel free to file a new issue with a reference to this issue. Add a description of detailed steps to reproduce, expected and current behaviour, logs and the output of 'flutter doctor -v'. Thanks for your contribution.