Please give me advice on measuring the cumulative travel distance of Android gps

Asked 2 years ago, Updated 2 years ago, 21 views

locationManager.requestLocationUpdates(
                                LocationManager.GPS_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);


After registering as location manager, you can use the onLocationChanged listener

        if(beforeLocation == null){
            beforeLocation = location;
        }else{
            distance += beforeLocation.distanceTo(location);
            beforeLocation = location;
        }

So I tried to measure the cumulative travel distance, but it got weird (Actually, when testing, beforeLocation = location; I missed this part, but as I went farther and farther, the distance traveled would have to increase significantly, but it went up too little.) Did I code it wrong?

android

2022-09-21 16:51

1 Answers

It seems that the accuracy decreases if the distance traveled is longer. Gangnam Station 37.4979° N, 127.0276° E Yeoksam Station 37.5008° N, 127.0369° E If you get the distance with that, you'll get 1053 meters well.

If you find the distance between Yeoksam Station and Busan (35.1796° N, 129.0756° E), you will get 277km. The straight distance should be at least 300km.

I ran the app on the Android emulator and transferred the virtual location to the app with the Android device monitor.

The LocationListener made the following:

private LocationListener locationListener = new LocationListener() {

    double distance=0;
    @Override
    public void onLocationChanged(Location location) {
        LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        // // Get the last location.
        if(lastKnownLocation==null) {
            lastKnownLocation = location;
        }
        else {
            distance=lastKnownLocation.distanceTo(location);
            Log.i("Distance","Distance:"+distance);
            lastKnownLocation=location;
        }
    }

        @Override
        public void onProviderDisabled(String provider) {}

        @Override
        public void onProviderEnabled(String provider) {}

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {}
    };

And it worked as below.

LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        lm.requestLocationUpdates(
                LocationManager.GPS_PROVIDER,
                1000,
                10,
                locationListener
        );

For your information, I haven't tested the part that calculates the cumulative travel distance.


2022-09-21 16:51

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.