为什么getSpeed​​()总是在Android返回0 [英] why getSpeed() always return 0 on android

查看:928
本文介绍了为什么getSpeed​​()总是在Android返回0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要的速度和GPS方位。然而,只有一些我从 location.getSpeed​​() 0或有时不可用。我的code:

I need to get the speed and heading from the gps. However the only number i have from location.getSpeed() is 0 or sometimes not available. my code:

        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;
}

我试着打与标准,但没有成功。有没有人有一个想法是什么问题?

I tried to play the Criteria with but no success. Does anyone have an idea what is the problem?

推荐答案

location.getSpeed​​()只返回了什么设置location.setSpeed​​()。这是一个值,你可以为某个位置对象设置。

location.getSpeed() only returns what was set with location.setSpeed(). This is a value that you can set for a location object.

要计算使用GPS的速度,你就必须做一些数学:

To calculate the speed using GPS, you'll have to do a little math:

Speed = distance / time

所以,你需要做的:

So you would need to do:

(currentGPSPoint - lastGPSPoint) / (time between GPS points)

全部转换为英尺/秒,或者不管你要显示的速度。这是我做的,当我做了一个亚军的应用程序。

All converted to ft/sec, or however you want to show the speed. This is how I did it when I made a runner app.

更具体地讲,你需要计算绝对距离:

More specifically, you'll need to calculate for absolute distances:

(sqrt((currentGPSPointX - lastGPSPointX)^2) + (currentGPSPointY - lastGPSPointY)^2)) / (time between GPS points)

这可能有助于使一个新的TrackPoint类或什么的,这使它被带到里面的GPS位置和时间。

It might help to make a new TrackPoint class or something, which keeps the GPS location and time it was taken inside.

这篇关于为什么getSpeed​​()总是在Android返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆