gpt4 book ai didi

ios - 用户移动时到某个位置的距离

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:51:51 25 4
gpt4 key购买 nike

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

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

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

最佳答案

您是否过滤掉旧的(缓存的)位置?您还应该根据准确性进行过滤,您可能不想要低准确性的位置。

您不会获得连续或定期更新,回调仅在位置发生变化时发生。

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

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

...
}

关于ios - 用户移动时到某个位置的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8247820/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com