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