简体   繁体   中英

How to precisely calculate speed and distance using GPS? Tracking user location. App for running

I want to make an application for running which tracks user location, speed and beaten distance. Currently I am using Google Location Services API to get GPS data, because Google recommends it on the official site .

But the problem is that I can recive location updates only every 10 seconds, which is OK if it comes only to drawning location on the map, but I want to calculte user current speed(km/h), current pace (min/m) and calculate exact time when he makes one lap (1km or 1 mile). And here is the problem if I am getting updates every 10sec I am not able to calculate precisely those parameters. For example, the update comes when user is on 996m and in 4m he will make one lap. But the next update will not be available for 10 sec, so it will come on eg 1010m. And that is not what user would like to have...

How to solve this problem? What do you think?

I am using the exact code form here.

Where location request looks like:

LocationRequest mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(10000);
mLocationRequest.setFastestInterval(5000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

From docs :

The priority of PRIORITY_HIGH_ACCURACY, combined with the ACCESS_FINE_LOCATION permission setting that you've defined in the app manifest, and a fast update interval of 5000 milliseconds (5 seconds), causes the fused location provider to return location updates that are accurate to within a few feet. This approach is appropriate for mapping apps that display the location in real time.

But it isn't enough. Even though I change interval for smaller it always is 10sec.

Use this code it can send Location updates every 3 seconds.

LocationListener listener = new LocationListener() {
        @Override
        public void onLocationChanged(final Location location) {
            //This will be called when location is changed


        }

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

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

            Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(i);

        }
    };

    locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

    }
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 0, listener);

More details of this code on Github.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM