why getSpeed() always return 0 on android(为什么 getSpeed() 在 android 上总是返回 0)
问题描述
我需要通过 GPS 获取速度和航向.但是,我从 location.getSpeed() 获得的唯一数字是 0 或有时不可用.我的代码:
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
所以你需要这样做:
(currentGPSPoint - lastGPSPoint) / (time between GPS points)
全部转换为 ft/sec,或者您想要显示的速度.这就是我在制作跑步者应用时的做法.
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么 getSpeed() 在 android 上总是返回 0
基础教程推荐
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
