[英]How to precisely calculate speed and distance using GPS? Tracking user location. App for running
我想制作一個可以跟蹤用戶位置,速度和拍打距離的應用程序。 目前,我正在使用Google Location Services API來獲取GPS數據,因為Google 在官方網站上推薦了該數據。
但是問題是我只能每10秒獲取一次位置更新,如果只涉及在地圖上繪制位置,這是可以的,但是我想計算用戶當前的速度(km / h),當前的步速(min / m)並計算出他跑一圈(1公里或1英里)的確切時間。 這就是問題,如果我每10秒獲取一次更新,則我將無法精確計算這些參數。 例如,當用戶在996m上時更新發生,而在4m中他將跑一圈。 但是下一次更新將在10秒鍾內不可用,因此它將持續1010m。 那不是用戶想要的...
如何解決這個問題呢? 你怎么看?
位置請求如下所示:
LocationRequest mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(10000);
mLocationRequest.setFastestInterval(5000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
從文檔 :
PRIORITY_HIGH_ACCURACY的優先級,再加上您在應用清單中定義的ACCESS_FINE_LOCATION權限設置,以及5000毫秒(5秒)的快速更新間隔,使融合的位置提供者返回准確的位置更新。腳。 此方法適用於實時顯示位置的地圖應用程序。
但這還不夠。 即使我將間隔更改為較小,也始終為10秒。
使用此代碼,它可以每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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.