我正在尝试在Google Glass live卡上显示用户的速度。 我能够获得纬度和经度,但getSpeed()总是返回0.0。我在SO上检查了类似的问题,但没有帮助。
这是我的代码
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
LocationManager mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
String provider = mLocationManager.getBestProvider(criteria, true);
boolean isEnabled = mLocationManager.isProviderEnabled(provider);
if (isEnabled) {
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
if (location != null) {
Geocoder geocoder = new Geocoder(Start_service.this.getBaseContext(), Locale.getDefault());
// lat,lng, your current location
List<Address> addresses = null;
try {
lati= location.getLatitude();
longi=location.getLongitude();
speed=location.getSpeed();
addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
}
catch (IOException e) {
System.out.println(e);
e.printStackTrace();
}
答案 0 :(得分:1)
Location providers don't guarantee to provide the speed value.您只能从调用setSpeed的提供程序中获取getSpeed。您可以设置Criteria变量以指示您需要速度值。
Criteria criteria = new Criteria();
criteria.setSpeedRequired(true);
或者您可以考虑自己计算一下。见why getSpeed() always return 0 on android。
另外,使用hasSpeed()检查速度是否可用。