關于定位一些參數(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];

就可以很好的做到這一點。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容