CLLocation 返回负速度

发布于 2024-10-05 23:13:22 字数 1746 浏览 3 评论 0 原文

我正在启动一个使用 coreLocation 和 mapkit 框架的新应用程序。

我的问题是尝试获取当前速度总是返回负值,我已经带着我的 iPhone 去了 3g 信号良好的地方,这并不重要,location.speed 值始终为 -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:

- (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;
    }



}

最后在我实现协议的视图控制器中:

- (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];
}

我快疯了,所以如果有人知道任何解决方案,那肯定会有帮助。 谢谢

I'm starting a new app which uses the coreLocation and the mapkit frameworks.

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.

here is the code which matters:

#define kRequiredAccuracy 1500.0 //meters
#define kMaxAge 60.0 //seconds

in the init method:

self.locationManager=[[CLLocationManager alloc] init];
self.locationManager.delegate=self;
self.locationManager.desiredAccuracy=kCLLocationAccuracyNearestTenMeters;

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

む无字情书 2024-10-12 23:13:22

通常,如果使用的位置检测机制不支持,您会得到负高度和速度。例如,当使用 Wi-Fi 或小区三角测量时。您确定正在获取 GPS 更新吗?您是在自由天空下进行测试吗?即使如此,您仍然会收到 WiFi 和手机信号塔位置更新,因此您必须将其过滤掉。

Wi-Fi 位置更新的典型水平精度为 65 m。

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.

65 m horizontal accuracy is typical for Wi-Fi location updates.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文