2

我使用google api客戶端進行位置更新,並始終返回速度0,但位置座標即將到來。我已經在摩托羅拉X2測試了這個應用雖然它在其他Android版本中正常工作。Android M Google api客戶端位置速度始終爲0,location.hasSpeed爲假

LocationRequest request = LocationRequest.create(); request.setPriority(Thresholds.PRIORITY_HIGH); // request.setInterval(GlobalData.GPS_INTERVAL); request.setFastestInterval(GlobalData.GPS_INTERVAL); LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, request, this) 

任何幫助,將不勝感激:)。

+0

林。我永遠花了我的時間來發現問題。嘗試打開谷歌地圖以及你自己的應用程序,你會看到職位與速度附加。我還沒有找到解決方案。你有嗎? – Lisandro

回答

0

我知道這有點遲,但對於那些仍然對這個解決方案感興趣的人來說,這是我的。基本上在每個位置變化中,我使用以下方法計算瞬時速度。由於GPS波動,它不是100%準確的,但它是一個開始。基本上你需要計算圖的斜率,所以你必須在兩個不同的時間點x1和x2中取兩個點y1和y2兩個點。具有完全相同的問題的瞬時速度由公式DY導出/ DX,其中DY = Y2-Y1和DX = X2 -x1

private double calculateInstantaneousSpeed(Location location) { 



    double insSpeed = 0; 
    if (y1 == null && x1 <= -1) { 
     //mark the location y1 at time x1 
     y1 = location; 
     x1 = duration.getDurationAsSeconds(); 


    } else { 
     //mark the location y2 at time x2 
     y2 = location; 
     x2 = duration.getDurationAsSeconds(); 


     //calculate the slope of the curve (instantaneous speed) 
     dy = y1.distanceTo(y2); 
     dx = x2 - x1; 

     insSpeed = dy/dx; 

     y1 = y2; 
     x1 = x2; 

    } 

    Singleton.getInstance().instantaneousSpeedSamples.add(insSpeed); 
    //System.out.println("Instantaneous Speed m/s: "+insSpeed); 
    return insSpeed; 
}