为什么getSpeed()总是在android上返回0

use*_*539 20 performance gps android location

我需要从gps获得速度和前进.但是我唯一的号码location.getSpeed()是0或有时不可用.我的代码:

        String provider = initLocManager();
    if (provider == null)
        return false;
    LocationListener locListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            updateWithNewLocation(location, interval, startId);
            Log.i(getString(R.string.logging_tag), "speed =" + location.getSpeed());
        }

        public void onProviderDisabled(String provider){
            updateWithNewLocation(null, interval, startId);
        }

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

    _locManager.requestLocationUpdates(provider, interval,  DEFAULT_GPS_MIN_DISTANCE, locListener);


    private String initLocManager() {
    String context = Context.LOCATION_SERVICE;
    _locManager = (LocationManager) getSystemService(context);

    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(true);
    criteria.setSpeedRequired(true);
    criteria.setCostAllowed(true);
    //criteria.setPowerRequirement(Criteria.POWER_LOW);
    String provider = _locManager.getBestProvider(criteria, true);

    if (provider == null || provider.equals("")) {
        displayGPSNotEnabledWarning(this);
        return null;
    }

    return provider;
}
Run Code Online (Sandbox Code Playgroud)

我尝试使用Criteria但没有成功.有谁知道这是什么问题?

Joh*_*hey 20

location.getSpeed()仅返回使用location.setSpeed()设置的内容.这是您可以为位置对象设置的值.

要使用GPS计算速度,您需要做一些数学运算:

Speed = distance / time
Run Code Online (Sandbox Code Playgroud)

所以你需要这样做:

(currentGPSPoint - lastGPSPoint) / (time between GPS points)
Run Code Online (Sandbox Code Playgroud)

全部转换为英尺/秒,或者您想要显示速度.这就是我制作跑步者应用程序时的方法.

更具体地说,您需要计算绝对距离:

(sqrt((currentGPSPointX - lastGPSPointX)^2) + (currentGPSPointY - lastGPSPointY)^2)) / (time between GPS points)
Run Code Online (Sandbox Code Playgroud)

制作一个新的TrackPoint类或其他东西可能有所帮助,它可以保持GPS的位置和时间.

  • 我已经看到getSpeed()返回零的问题,即使'hasSpeed()'为真并且有运动.在一个6.0.1设备上,它总是返回零!从融合位置服务.常规的LocationManager提供了速度,但在更新中似乎不太准确且频率较低.所以我按照自己的速度计算了. (3认同)

Kev*_*OUX 8

如果有速度,我的自定义LocationListener用于手动获取速度和位置对象.

 new LocationListener() {
        private Location mLastLocation;

        @Override
        public void onLocationChanged(Location pCurrentLocation) {
            //calcul manually speed
            double speed = 0;
            if (this.mLastLocation != null)
                speed = Math.sqrt(
                        Math.pow(pCurrentLocation.getLongitude() - mLastLocation.getLongitude(), 2)
                                + Math.pow(pCurrentLocation.getLatitude() - mLastLocation.getLatitude(), 2)
                ) / (pCurrentLocation.getTime() - this.mLastLocation.getTime());
            //if there is speed from location
            if (pCurrentLocation.hasSpeed())
                //get location speed
                speed = pCurrentLocation.getSpeed();
            this.mLastLocation = pCurrentLocation;
            ////////////
            //DO WHAT YOU WANT WITH speed VARIABLE
            ////////////
        }

        @Override
        public void onStatusChanged(String s, int i, Bundle bundle) {

        }

        @Override
        public void onProviderEnabled(String s) {

        }

        @Override
        public void onProviderDisabled(String s) {

        }
    };
Run Code Online (Sandbox Code Playgroud)


Niv*_*nça 7

Imbru答案看起来非常好,但是如果你正在与单位合作,那就没有用了.

这就是我用来计算速度(米/秒)(m/s)的方法.

new LocationListener() {
    private Location lastLocation = null;
    private double calculatedSpeed = 0;

    @Override
    public synchronized void onLocationChanged(Location location) {
        if (lastLocation != null) {
            double elapsedTime = (location.getTime() - lastLocation.getTime()) / 1_000; // Convert milliseconds to seconds
            calculatedSpeed = lastLocation.distanceTo(location) / elapsedTime;
        }
        this.lastLocation = location;

        double speed = location.hasSpeed() ? location.getSpeed() : calculatedSpeed;

        /* There you have it, a speed value in m/s */

        . . .

    }

    . . .

}
Run Code Online (Sandbox Code Playgroud)

  • 仅当location.hasSpeed()== false时才进行速度计算。有人还报告说,即使location.hasSpeed()== true,location.getSpeed()总是返回0。因此,我将使用“ location.hasSpeed()&& location.getSpeed()> 0”作为条件。 (2认同)

小智 5

在球形行星上,应使用以下公式计算距离:

private static Double distance(Location one, Location two) {
       int R = 6371000;        
       Double dLat = toRad(two.getLatitude() - one.getLatitude());
       Double dLon = toRad(two.getLongitude() - one.getLongitude());
       Double lat1 = toRad(one.getLatitude());
       Double lat2 = toRad(two.getLatitude());         
       Double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
               + Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2);        
       Double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));        
       Double d = R * c;
       return d;
   }
private static double toRad(Double d) {
       return d * Math.PI / 180;
   }
Run Code Online (Sandbox Code Playgroud)

  • 如果用户在斜坡上怎么办? (2认同)