简体   繁体   English

如何使用GPS精确计算速度和距离? 跟踪用户位置。 运行应用

[英]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 . 目前,我正在使用Google Location Services API来获取GPS数据,因为Google 在官方网站上推荐了该数据。

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). 但是问题是我只能每10秒获取一次位置更新,如果只涉及在地图上绘制位置,这是可以的,但是我想计算用户当前的速度(km / h),当前的步速(min / m)并计算出他跑一圈(1公里或1英里)的确切时间。 And here is the problem if I am getting updates every 10sec I am not able to calculate precisely those parameters. 这就是问题,如果我每10秒获取一次更新,则我将无法精确计算这些参数。 For example, the update comes when user is on 996m and in 4m he will make one lap. 例如,当用户在996m上时更新发生,而在4m中他将跑一圈。 But the next update will not be available for 10 sec, so it will come on eg 1010m. 但是下一次更新将在10秒钟内不可用,因此它将持续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. PRIORITY_HIGH_ACCURACY的优先级,再加上您在应用清单中定义的ACCESS_FINE_LOCATION权限设置,以及5000毫秒(5秒)的快速更新间隔,使融合的位置提供者返回准确的位置更新。脚。 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. 即使我将间隔更改为较小,也始终为10秒。

Use this code it can send Location updates every 3 seconds. 使用此代码,它可以每3秒发送一次位置更新。

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. 该代码在 Github 上的更多详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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