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

我需要从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; } 

我试图发挥标准,但没有成功。 有没有人有一个想法是什么问题?

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的位置和时间。

有我的自定义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) { } }; 

在一个球形行星的距离应该用这些公式来计算:

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

Imbru的答案看起来非常好,但是如果你正在与单位合作,这不是很有帮助。

这就是我所做的计算米/秒(米/秒)的速度。

 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 */ . . . } . . . } 

getspeed()工作正常。 你不必使用距离公式来做math运算。 它已经在那里,只要有经纬度,就会有速度。

(1)我相信你可以使用requestLocationUpdates()方法,然后创build一个LocationListener类,其中onLocationChange方法被设置为显示getSpeed() 。 这是我最近看到它完成与Location.getLatitude和Location.getLongitude,所以我相信你可以使用getSpeed()相同的方式,正确的?

(2)刚读完eclipse描述窗口后,我看到前面的人说的是:“如果hasSpeed()是false,则返回0.0f”。 但也许这将有助于: http ://www.ehow.com/how_5708473_convert-latitude-feet.html 🙂

我之前也遇到过这个问题,希望对此有所帮助。

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

我试图得到使用旧的联想设备的速度,它返回0,因为它不能locking一个GPS。

我尝试使用三星galaxy nexus和它返回我的速度(有一个更好的GPS传感器)。

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

我basicaslly计算瞬时速度,然后使用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; }