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

时间:2011-01-27 00:54:11

标签: android gps performance 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;
}

我尝试播放Criteria但没有成功。有谁知道这是什么问题?

11 个答案:

答案 0 :(得分:19)

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

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

Speed = distance / time

所以你需要这样做:

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

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

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

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

创建一个新的TrackPoint类可能有所帮助,它可以保持GPS的位置和时间。

答案 1 :(得分: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) {

        }
    };

答案 2 :(得分:7)

Imbru' answer看起来非常好,但如果你正在与单位合作,那就没有用了。

这就是我用来计算每秒米速(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 */

        . . .

    }

    . . .

}

答案 3 :(得分:4)

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

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

答案 4 :(得分:1)

(1)我相信您可以使用requestLocationUpdates()方法,然后创建一个LocationListener类,并将onLocationChange方法设置为显示getSpeed()。这就是我最近用Location.getLatitude和Location.getLongitude看到它的方式,所以我相信你可以用同样的方式使用getSpeed(),对吗?

(2)但是,在阅读了eclipse描述窗口后,我看到它确切地说出了前一个人所说的:“如果hasSpeed()为false,则返回0.0f。”      但也许这会有所帮助:http://www.ehow.com/how_5708473_convert-latitude-feet.html:)

答案 5 :(得分:1)

getspeed()工作正常。您不必使用距离公式进行数学运算。它已经存在于空中,只要有纬度和经度,就会有速度的速度。

答案 6 :(得分:1)

getSpeed()方法实际上可以正常工作,但是您必须使用较高的请求间隔(例如1秒),并且需要较高的精度,首先我正在执行3秒的间隔,并且PRIORITY_BALANCED_POWER_ACCURACY并且我一直得到0的值,直到我更改它,例如。我正在使用融合的位置提供程序api。

public class Main3Activity extends AppCompatActivity {

private FusedLocationProviderClient mFusedLocationClient;
private int speed;
private double lat;
private double lng;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
}

@Override
protected void onResume() {
    super.onResume();
    if(!runtime_permissions()) {
        requestLocations();
    }
}

@Override
protected void onPause() {
    super.onPause();
    //stop location updates when Activity is no longer active
    if (mFusedLocationClient != null) {
        mFusedLocationClient.removeLocationUpdates(mLocationCallback);
    }
}

@SuppressLint("MissingPermission")
private void requestLocations(){
    LocationRequest mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(1000);;
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
}

LocationCallback mLocationCallback = new LocationCallback() {
    @Override
    public void onLocationResult(LocationResult locationResult) {
        List<Location> locationList = locationResult.getLocations();
        if (locationList.size() > 0) {
            //The last location in the list is the newest
            Location location = locationList.get(locationList.size() - 1);

            lat = location.getLatitude();
            lng = location.getLongitude();

            //speed in km/h
            speed = (int) ((location.getSpeed() * 3600) / 1000);

        }
    }
};

private boolean runtime_permissions() {
    if(Build.VERSION.SDK_INT >= 23 && ContextCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){

        requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},100);

        return true;
    }
    return false;
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if(requestCode == 100){
        if( grantResults[0] == PackageManager.PERMISSION_GRANTED){
            onResume();
        }else{
            runtime_permissions();
        }
    }
}

}

答案 7 :(得分:0)

之前我也遇到过这个问题, 我希望这可以提供帮助。

它返回0,因为您的设备无法锁定GPS,或无法连接到GPS。

我尝试使用较旧的lenovo设备获得速度,并且它返回0,因为它无法锁定gps。

我尝试使用三星Galaxy nexus并且它恢复了我的速度(有更好的GPS传感器)。

手机中的GPS传感器可能不太好,或者您所在的GPS信号较弱的区域(如房屋或建筑物内)。

答案 8 :(得分:0)

我基本上可以计算瞬时速度,然后使用setSpeed()方法将其添加到该位置。它非常准确,因为我在一辆车内进行了比较,我可以检查测速仪。

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

答案 9 :(得分:0)

嘿,我也遭受了同样的痛苦,但是现在我已经解决了! 只需将值乘以18/5即可得到几乎准确的值。

speed=location.getSpeed()*18/5

也将interval指定为1000*2,将fastest interval指定为1000*1,以提高准确性

答案 10 :(得分:0)

根据此处的其他建议,我将此代码与注释的原因放在一起:

public static float getSpeedKmh(Location newLocation, Location previousLocation)
{
    if(newLocation == null) throw new IllegalArgumentException("newLocation must not be null");
    float speed = -1.0f;
    if(newLocation.hasSpeed()) /* gps doppler speed at the current moment preferred */
    {
        speed = newLocation.getSpeed();
    }else /* may be wifi or celltower based location: compute AVERAGE speed since last fix */
    {
       if(previousLocation == null) throw new IllegalArgumentException("Cannot compute speed without previousLocation (was null)");
       if(previousLocation.getTime()==newLocation.getTime())
           throw new IllegalArgumentException("Cannot compute speed from same time locations!"); /* diff by zero protection */
       speed = newLocation.distanceTo(previousLocation) * 1000.0f /* time diff in millis so multiply by 1000 */
           / Math.abs(newLocation.getTime() - previousLocation.getTime()); /* Math abs: so you can even swap new and older location without effect */
    }
       return speed * 3.6f; /* m/s -> km/h */
}