用户在运动时与某个位置的距离 [英] Distance to a location while user is in motion

查看:81
本文介绍了用户在运动时与某个位置的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个应用程序,在用户走动时显示用户与固定点的距离(即,每次用户移动时,更新显示用户到该点的距离的标签)。我使用CLLocationManager,代码如下所示:

I'm in the process of writing an application that shows the user's distance from a fixed point as the user walks around (i.e. the label showing the distance from the user to the point is updated every time the user moves). I use a CLLocationManager with the code shown below:

- (void)viewDidLoad
{
    locationManager=[[CLLocationManager alloc]init]; 
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    [locationManager startUpdatingLocation];      
}

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation      *)newLocation fromLocation:(CLLocation *)oldLocation 
{   
    CLLocationDistance meters = [newLocation distanceFromLocation:fixedPoint];
    self.distanceLabel.text = [[NSString alloc] initWithFormat:@"Distance: %.1f feet", meters*3.2808399];
}

应该显示用户到点的距离的标签是不断更新,当它更新时,它通常不会显示从用户到固定点的正确距离。我想知道是否有更好的方法让我尝试这样做,或者核心位置框架的基本限制是否使这变得不可能。任何帮助将不胜感激。

The label that is supposed to show the distance from the user to the point isn't updated constantly and when it is updated, it doesn't usually show the correct distance from the user to the fixed point. I was wondering if there is a better way for me to try and do this, or do the fundamental limitations of the core location framework make this impossible. Any help will be greatly appreciated.

推荐答案

您是否过滤掉旧的(缓存)职位?您还应该根据准确度进行过滤,您可能不希望低精度位置。

Are you filtering out old (cached) positions? You should also filter based on accuracy, you probably don't want low accuracy locations.

您不会连续或定期更新,只有在位置发生变化时才会发生回调。

You won't get continous or periodic update, the callback only occurs when the location has changed.

假设设备有GPS并且可以看到足够的GPS卫星以获得好位置,这样可以正常工作。

Assuming the device has GPS and can see enough GPS satellites to get a good position, this works fine.

-(void)locationManager:(CLLocationManager *)manager 
   didUpdateToLocation:(CLLocation *)newLocation 
          fromLocation:(CLLocation *)oldLocation {

    NSTimeInterval age = -[newLocation.timestamp timeIntervalSinceNow]; 

    if (age > 120) return;    // ignore old (cached) updates

    if (newLocation.horizontalAccuracy < 0) return;   // ignore invalid udpates

    ...
}

这篇关于用户在运动时与某个位置的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆