关于定位一些参数(distance,speed,course)为Negative的问题

最近在做语音导航的一款App,有个需求要实时显示目前的速度,真正做的时候才发现 CLLocation对象的speed属性和course属性经常为-1。

这让我很好奇,查阅官网文档发现:

Returns the speed of the location in m/s. Negative if speed is invalid.

原来负值代表着无效参数,但是什么引起了无效参数呢?在Stack Overflow上翻了半天以后,看到一段非常有价值的解释:

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.

结合翻得其他问题得出结论:

1  CLLocation对象下speed or course属性的值有精度要求,如果你CLLocationManager对象的desiredAccuracy属性设置为kCLLocationAccuracyBest,亦或者distanceFilter属性设置太小,将会导致didUpdateLocations在很短的时间内(推测为1秒内)多次调用,导致不能计算出实时的速度以及方向;

2  CLLocation对象下speed or course属性要求GPS信息,而非WI-FI或者手机蜂窝数据网络信息,设备通常回自动选择最适合现在的导航方式,默认是GPS>WI-FI>蜂窝数据,在测试的时候可以去露天的地方测试;

3  您也可以记录上旧的经纬坐标,用以下方法来判断是否有效:

-(BOOL)isValidLocation:(CLLocation*)newLocationwithOldLocation:(CLLocation*)oldLocation{

if(!newLocation){return NO;}

if(newLocation.horizontalAccuracy<0){return NO;}

NSTimeIntervalsecondsSinceLastPoint = [newLocation.timestamp timeIntervalSinceDate:oldLocation.timestamp];

if(secondsSinceLastPoint<0){return NO;}

return YES;

}

如果返回为YES则计算距离distance,速度speed,方向course更可能是有效值;

如果想做到适应性更好的速度显示,不妨用两次坐标的时间差值和坐标的距离来计算速度;

[currentLocationdistanceFromLocation:oldLocation];

就可以很好的做到这一点。

你可能感兴趣的:(关于定位一些参数(distance,speed,course)为Negative的问题)