admin管理员组文章数量:1023204
i want to get the speed of a moving car using a flutter app.
i'm using geolocator ,the accuracy of the speed is pretty high , but the frequency of the changes is pretty low , sometimes i have to wait 10s to get the new change in speed even though the car is moving 40km/h , how can i fix that , i want it to change maybe every 2-4s max ?
class _MyHomePageState extends State<MyHomePage> {
late StreamSubscription<Position> _positionStream;
double _speed = 0.0;
@override
void initState() {
super.initState();
_startTracking();
}
void _startTracking() async {
bool serviceEnabled;
LocationPermission permission;
// Check if location services are enabled
serviceEnabled = await Geolocator.isLocationServiceEnabled();
if (!serviceEnabled) {
return; // Location services are not enabled
}
// Check location permissions
permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
if (permission == LocationPermission.denied) {
return; // Permissions are denied
}
}
if (permission == LocationPermission.deniedForever) {
return; // Permissions are permanently denied
}
// Start listening to position updates
_positionStream = Geolocator.getPositionStream(
locationSettings: const LocationSettings(
distanceFilter: 8,
accuracy: LocationAccuracy.best,
),
).listen((Position? position) {
if (position != null) {
_onSpeedChange((position.speed * 18) / 5); // Convert m/s to km/h
}
});
}
void _onSpeedChange(double newSpeed) {
setState(() {
_speed = newSpeed < 3.5 ? 0.0 : newSpeed;
});
}
@override
void dispose() {
_positionStream.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'Current_Speed:',
style: TextStyle(fontSize: 20),
),
Text(
'${_speed.toStringAsFixed(0)} km/h',
style: const TextStyle(fontSize: 32, fontWeight: FontWeight.bold),
),
],
),
),
);
}
}
i want to get the speed of a moving car using a flutter app.
i'm using geolocator ,the accuracy of the speed is pretty high , but the frequency of the changes is pretty low , sometimes i have to wait 10s to get the new change in speed even though the car is moving 40km/h , how can i fix that , i want it to change maybe every 2-4s max ?
class _MyHomePageState extends State<MyHomePage> {
late StreamSubscription<Position> _positionStream;
double _speed = 0.0;
@override
void initState() {
super.initState();
_startTracking();
}
void _startTracking() async {
bool serviceEnabled;
LocationPermission permission;
// Check if location services are enabled
serviceEnabled = await Geolocator.isLocationServiceEnabled();
if (!serviceEnabled) {
return; // Location services are not enabled
}
// Check location permissions
permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
if (permission == LocationPermission.denied) {
return; // Permissions are denied
}
}
if (permission == LocationPermission.deniedForever) {
return; // Permissions are permanently denied
}
// Start listening to position updates
_positionStream = Geolocator.getPositionStream(
locationSettings: const LocationSettings(
distanceFilter: 8,
accuracy: LocationAccuracy.best,
),
).listen((Position? position) {
if (position != null) {
_onSpeedChange((position.speed * 18) / 5); // Convert m/s to km/h
}
});
}
void _onSpeedChange(double newSpeed) {
setState(() {
_speed = newSpeed < 3.5 ? 0.0 : newSpeed;
});
}
@override
void dispose() {
_positionStream.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'Current_Speed:',
style: TextStyle(fontSize: 20),
),
Text(
'${_speed.toStringAsFixed(0)} km/h',
style: const TextStyle(fontSize: 32, fontWeight: FontWeight.bold),
),
],
),
),
);
}
}
本文标签: getting the speed of a moving car with a geolocator flutterStack Overflow
版权声明:本文标题:getting the speed of a moving car with a geolocator flutter - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745559984a2156106.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论