3

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 ?

Piyush Jain
  • 1,895
  • 3
  • 19
  • 40

2 Answers2

0

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();
nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • Thanks for your reply, How will I know that user is not moving ? because when user is moving I am listening to data (need current speed) and when user is not moving I need to show speed as 0 – Piyush Jain Aug 10 '20 at 06:28
  • I guess you need a timer? I don't know how you calculate speed, but you cannot calculate speed just from positions... it's always (position delta *per time unit*) for example km/h. – nvoigt Aug 10 '20 at 06:30
  • 1
    we can get current speed using location.speed. location package - https://pub.dev/packages/location `class LocationData { final double latitude; // Latitude, in degrees final double longitude; // Longitude, in degrees final double accuracy; // Estimated horizontal accuracy of this location, radial, in meters final double altitude; // In meters above the WGS 84 reference ellipsoid final double speed; // In meters/second final double speedAccuracy; // In meters/second, always 0 on iOS final double time; //timestamp of the LocationData }` – Piyush Jain Aug 10 '20 at 06:31
  • I am using same but I am getting that only when user is moving. Once user stops not getting data inside .listen(), that is my main concern. – Piyush Jain Aug 10 '20 at 06:34
  • And I just showed you how you can get data any time you want. Now you just have to figure out a way to set a timer and get the data yourself if your old data is "too old". – nvoigt Aug 10 '20 at 06:37
  • Pretty good examples of doing something on a timer: https://stackoverflow.com/questions/14946012/how-do-i-run-a-reoccurring-function-in-dart – nvoigt Aug 10 '20 at 06:40
0

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!

Devyank Shaw
  • 159
  • 7
  • Why is it giving non zero speed even if the device is not moving? – Abdullah Shahid Oct 24 '21 at 15:26
  • @AbdullahShahid for the first few seconds it will track your current location. So during that time, the speed may be non-zero. But once it's get's your accurate location. It will be zero. – Devyank Shaw Oct 25 '21 at 06:46