为什么我的 CLLocation 速度如此不准确? [英] Why is my CLLocation speed so inaccurate?

查看:13
本文介绍了为什么我的 CLLocation 速度如此不准确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I´m playing a bit with the iPhone SDK and I want to show the current speed in my application. There are so many apps that can do that really precise, especially for low speeds like running or biking. The best I've seen is RunKeeper.

But, in my application, the speed is absolutely inaccurate. In low speeds it is always null, and only at higher speeds it shows some values but they are rarely updated and not really useful.

- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
       fromLocation:(CLLocation *)oldLocation {
    if (newLocation.timestamp > oldLocation.timestamp &&
        newLocation.verticalAccuracy > 0.0f &&                                  // GPS is active
        newLocation.horizontalAccuracy < 500.0f &&                                  // GPS is active
        //newLocation.horizontalAccuracy < kCLLocationAccuracyHundredMeters &&  // good quality GPS signal
        //newLocation.speed > 1.0f &&                                           // enough movment for accurate speed and course measurement
        oldLocation != nil)                                                     // oldLocation is nil on the first reading
    {
        double speed = (newLocation.speed * 3.6);
        [self updateDisplayWithSpeed:speed];
        //double direction = newLocation.course;
    }
}

Does anyone have working code for that? Or can you tell me what´s wrong with mine?

解决方案

I would try the following.

Remember that you should set the distanceFilter and desiredAccuracy as needed by your scenario: walking is not the same as traveling by car etc. Moreover, when you request high accuracy from the GPS, you must always discard the very first location provided by the GPS and use the second one as the starting location. Quoting the Apple documentation:

You should assign a value to this property that is appropriate for your usage scenario. In other words, if you need only the current location within a few kilometers, you should not specify kCLLocationAccuracyBest for the accuracy. Determining a location with greater accuracy requires more time and more power. When requesting high accuracy location data, the initial event delivered by the location service may not have the accuracy you requested. The location service delivers the initial event as quickly as possible. It then continues to determine the location with the accuracy you requested and delivers additional events, as necessary, when that data is available.

Here is my suggestion.

First, update the location using the GPS using an interval of at least one second. Below this interval the speed value is useless because it is measured in meters per second and owing to the previous discussion, you are not likely to get a valid update in less than a second with high accuracy.

Second, only use meaningful values for the location coordinates: you should discard values with negative horizontalAccuracy. This is what I am referring to when speaking of good locations.

Third, you can compute the distance by yourself: compute the distance between the last good location and the previous good location using getDistanceFrom() and then divide by the number of seconds elapsed between the location update. This will give you the distance in meters per second. I will try to do this and compare the result with the Apple provided speed.

这篇关于为什么我的 CLLocation 速度如此不准确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆