开发者

android speed using COARSE

hi to all how can i get the speed using t开发者_运维问答he ACCESS_COARSE_LOCATION in android

thank you


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜