为什么我的 CLLocation 速度如此不准确?我的、不准确、速度、CLLocation

2023-09-07 04:36:58 作者:别碰我心

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.