使用 COARSE 的 android 速度

发布于 2024-10-24 17:36:47 字数 77 浏览 2 评论 0原文

大家好 我怎样才能在android中使用ACCESS_COARSE_LOCATION获得速度

谢谢

hi to all
how can i get the speed using the ACCESS_COARSE_LOCATION in android

thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

破晓 2024-10-31 17:36:47

您好,您指的是 Location.getSpeed() 我相信这是一个可选字段,可能取决于设备是否真正支持它。首先尝试一下,

Criteria criteria = new Criteria();
criteria.setSpeedRequired(true);
LocationProvider[] providers = mLocationManager.getProviders(criteria, false);

if (providers == null || providers.length == 0) {
 //Sorry it doesn't do it by default.
}

幸运的是,数学和科学会帮助我们:) 只需计算从 LocationProviders 获取更新时的速度即可。确保计算经纬度随时间变化的距离,作为两个位置更新记录的时间戳的差异。

...
onLocationChanged(Location location) {
 if (mOldLocation != null) {
   float distance = getDistance(mOldLocation, location);
   long time = location.getTime() - mOldLocation.getTime();
   mSpeed = distance/time;
 }
 mOldLocation = location;
}

它不一定非常准确(可能真的很吵),但制作柠檬水比吃腐烂的水果更好……希望这会有所帮助。确保也只比较同一提供商的增量。您可能会发现自己正在调整手动计算,因为设备提供商非常多样化,并且不能保证时间总是向前推进。

Hi are you referring to Location.getSpeed() I believe that is an optional field and probably up to the device to actually support it. First off try this

Criteria criteria = new Criteria();
criteria.setSpeedRequired(true);
LocationProvider[] providers = mLocationManager.getProviders(criteria, false);

if (providers == null || providers.length == 0) {
 //Sorry it doesn't do it by default.
}

Luckily Math and Science will help us :) Just calculate the speed as you get updates from your LocationProviders. Make sure to calculate the distance between the lat,longs over times as the difference of the recorded times stamps of two Location updates.

...
onLocationChanged(Location location) {
 if (mOldLocation != null) {
   float distance = getDistance(mOldLocation, location);
   long time = location.getTime() - mOldLocation.getTime();
   mSpeed = distance/time;
 }
 mOldLocation = location;
}

It won't necessarily be greatly accurate (probably really noisy) but it's better to make lemonade than to have rotted fruit ... hopefully this helps. Make sure to only compare deltas across the same provider too. You will probably find yourself tweaking the manual calculation because the devices providers are very diverse and don't guarantee that time always moves forward.

巷雨优美回忆 2024-10-31 17:36:47

最有可能的是,你不能。 “粗略”的定位技术只能知道你在几百米范围内的位置。您的速度可能会有很大差异。如果您需要知道您的速度,您很可能需要一个“精细”的位置提供商。

或者,更好的是,使用 Criteria 对象来规定您需要了解设备的速度,并让它引导您找到最佳的提供商来使用。

Most likely, you cannot. Location technology that is "coarse" only knows where you are within several hundred meters. Your speed could vary wildly. If you need to know your speed, you will most likely need a "fine" location provider.

Or, better yet, use the Criteria object to stipulate that you need to know the device's speed, and let it guide you to the best provider to use.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文