flutterlocation
flutterlocation copied to clipboard
It returns Speed, Speed Accuracy as -1.
Description I'm trying to get speed and speed accuracy information periodically from getLocation() but they return -1. I don't know how it works but even I move my phone forward, it returns -1. Other latitude, longitude, accuracy values are printed with no problem.
This is my code
class _TimerScreenState extends State<TimerScreen> {
Timer? timer;
int restMilliSec = 0;
int setCnt = 0;
@override
void initState() {
super.initState();
bool canVibrate;
WalkType currentType = WalkType.READY;
restMilliSec = decideTimerTime(currentType) * 1000;
timer = Timer.periodic(Duration(milliseconds: 1), (timer) async {
// ... other code for another logic... //
if (canVibrate) Vibrate.vibrate();
setLocation();
}
});
}
void setLocation() async {
Location location = Location();
LocationData _currentPosition = await location.getLocation();
print("accuracy : ${_currentPosition.accuracy}, speed: ${_currentPosition.speed}");
print("Speed Accuracy : ${_currentPosition.speedAccuracy}");
}
}
Expected behavior A clear and concise description of what you expected to happen. I guess it will return different positive value every time it's called.
Steps To Reproduce
Tested on:
I'm running my app in iOS real device, Version is iOS13 in debug mode. And I'm running it indoors. I'm using location ^4.4.0
Other plugins:
- flutter vibration
Additional logs

Hello :) Thanks for your report. can you try the latest version 5?
Thank you for your answer!
I tried location: ^5.0.0-dev.8 but I still have problems.
I want to keep track user's speed change so I used onLocationChanged()
but my iPhone device still says its speed is '-1' and also my Android device returns 0.
Even though I call getLocatoin() in timer, it returns same result.
Did I use the function incorrectly? I'll be so glad if anyone can help me.
This is my code for onLocationChanged().
setLocationSettings(
ignoreLastKnownPosition: true,
interval: 1000,
accuracy: LocationAccuracy.high,
smallestDisplacement: 0);
var location = onLocationChanged(inBackground: true).listen((event) {
setState(() {
speed = event.speed;
speedAccuracy = event.speedAccuracy;
});
print(
"${event.speedAccuracy} SPEED: ${event.speed}, Location: ${event.longitude} ${event.latitude}");
});
and code for getLocation().
final location = await getLocation(
settings: LocationSettings(
ignoreLastKnownPosition: true,
accuracy: LocationAccuracy.low,
interval: 1000,
smallestDisplacement: 0),
).then((res) {
setState(() {
speed = res.speed;
speedAccuracy = res.speedAccuracy;
});
print(
"${res.speedAccuracy} SPEED: ${res.speed}, Location: ${res.longitude} ${res.latitude}");
});
-
result of iOS real device

-
result of Android Emulator *Android device is failing onLocation

Have you tried outdoors with good GPS reception?
I am using location 4.4.0 on iPhone13 / iOS 16.2 and speed is working correctly.
Hi, please provide a minimal reproducible example so your problem can be debugged :)