問題描述
我正在啟動一個使用 coreLocation 和 mapkit 框架的新應用程序.
I'm starting a new app which uses the coreLocation and the mapkit frameworks.
我的問題是試圖獲得當前速度總是給我一個負值,我帶著我的 iPhone 到了 3g 信號良好的地方,沒關系,location.speed 值總是 -1.
My problem is trying to get the current speed always returns me a negative value, i have take my iPhone with me to places with a good 3g signal and it doesn't matter, location.speed value is always -1.
這是重要的代碼:
#define kRequiredAccuracy 1500.0 //meters
#define kMaxAge 60.0 //seconds
在init方法中:
self.locationManager=[[CLLocationManager alloc] init];
self.locationManager.delegate=self;
self.locationManager.desiredAccuracy=kCLLocationAccuracyNearestTenMeters;
然后didUpdateToLocation:
then didUpdateToLocation:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
NSTimeInterval ageInSeconds = [newLocation.timestamp timeIntervalSinceNow];
NSLog(@"Location: %@", [newLocation description]);
if( newLocation.horizontalAccuracy > kRequiredAccuracy || fabs(ageInSeconds) > kMaxAge )
{
NSLog(@"inacurate position");
[self.delegate inacuratePosition];
}
else {
[self.delegate locationUpdate:newLocation andOldLocation:oldLocation];
location=newLocation.coordinate;
}
if(tracking)
{
[self updatePosition];
}
if(firstTime)
{
[self placeStartMark];
firstTime=FALSE;
}
}
最后在我實現協議的視圖控制器中:
and finally in the view controller in which I'm implementing the protocol:
- (void)locationUpdate:(CLLocation *)newLocation andOldLocation:(CLLocation*)oldLocation{
double speed = [newLocation speed] *3.6;
double altitude= [newLocation altitude];
[self checkMaxSpeedAndAltitude:speed :altitude];
if(speed< 0)
speed=0;
locationLabel.text=[NSString stringWithFormat:@"speed: %f km/h altitude: %f m", speed,altitude];
}
我快瘋了,所以如果有人知道任何解決方案,那肯定會有所幫助.謝謝
Im getting crazy so if anyone knows any solution it will be helpful for sure. Thanks
推薦答案
通常,如果使用的位置檢測機制不支持,您會得到負高度和速度.例如,當使用 Wi-Fi 或小區三角測量時.你確定,你正在獲取 GPS 更新,所以你是在自由的天空下測試嗎?即便如此,您仍會收到 WiFi 和手機信號塔位置更新,因此您必須將它們過濾掉.
Usually, you get negative altitude and speed if the used location detection mechanism doesn't support it. For instance, when Wi-Fi or cell triangulation is used. Are you sure, you're getting GPS updates, so are you testing under the free sky? Even then, you'll still receive WiFi and cell tower location updates so you've to filter them out.
Wi-Fi 位置更新的典型水平精度為 65 m.
65 m horizontal accuracy is typical for Wi-Fi location updates.
這篇關于CLLocation 返回負速度的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!