gpt4 book ai didi

iphone - 强制位置更新

转载 作者:行者123 更新时间:2023-12-03 20:45:49 26 4
gpt4 key购买 nike

我正在尝试克服 didUpdateToLocation 在根据当前位置检查本地数据、关闭应用程序、移动一段时间并再次打开应用程序时有时会失败的问题。发生的情况是,用户访问一个地方,检查列表,然后转到另一个地方,然后更新列表,但使用旧的位置数据。

我想确保我有一个新的新位置。

这段代码有什么问题?

double aa,bb,cc;

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
[manager stopUpdatingLocation]; //only stop if new reliable pos fetched.
//old data? retry.
aa=[newLocation.timestamp timeIntervalSinceReferenceDate];
bb=[NSDate timeIntervalSinceReferenceDate];
cc=bb-aa; //must be <60 - minute-fresh
if (cc>60) {
[manager stopUpdatingLocation]; //only stop if new reliable pos fetched.
[manager startUpdatingLocation]; //only stop if new reliable pos fetched.
return;
}
...handle position
}

症状是应用程序启动时 cc 大约需要 1200 秒,然后每次重试都会增加几秒。

我尝试过-[newLocation.timestamp timeIntervalSinceNow],类似地,每次重试都会增加几秒。在某一时刻,间隔约为 2400。

我正在使用 FilterNone 和 AccuracyBest,如果这会影响它。

我愿意接受替代代码解决方案,以确保您拥有新的职位。

最佳答案

答案是您需要按照您希望的行为方式编写处理。

我已经阅读了这个常见问题。我应该澄清一下,“强制”意味着启动明确的行为,从而在最佳条件下为手机提供可靠的位置。不要忙等待直到满意或期望立即得到结果/错误代码。

从常识的角度来看,所有开发人员都希望至少有一个额外的方法来简单地请求参数中的位置,并且仅在其有效且在准确性范围内、或取消或超时时回调。如果条件不允许,那么您无需测试深奥的行为即可使用自定义代码处理它。

为什么我需要问这个问题是:

  1. 不存在如上所述的方法

  2. 使用书中的代码(更多 Iphone 3 开发)

  3. 当从头开始编写它时(查看 LocateMe.xproj),发现:

  4. 模拟器行为与手机行为不同(不是指位置本身,这显然与 ip 查找一样好,而是指 didUpdateToLocation 的行为)。认识到模拟器的局限性,该方法至少应该像在设备上那样运行。但目前,正确编写的位置处理/检查代码只会在模拟器中超时(如 LocateMe 示例),而错误的代码(询问一次并在回调中使用 newLocation)则有效。

  5. 以及一个导致 didUpdateToLocation 在 [locationManager stopUpdatingLocation] 之后被调用两次的错误;

如果除此之外,您(像我一样)根据您的位置加载数据,并且应用程序中的多个 View 需要这些数据,则 Apple 的位置获取方法的行为并不容易处理该链更新-加载-计算-在整个应用程序中一致且无问题地呈现。特别是如果您陷入用户/老板的看法或决定如何工作(困难)和系统如何工作(困难)之间。

这是我当前在设备和模拟器上运行的代码:

//ask for a position, as fresh as possible.
-(void)updateMeMap {
lastloc.coordinate=homeloc.coordinate;
lm.delegate=self;
ctr=0;
[self performSelector:@selector(stopUpd) withObject:nil afterDelay:gpstimeout];
[lm startUpdatingLocation];
}

//called on each position update.
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
ctr++;
if (ctr<15) {
NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
NSLog(@"%d",ctr);
if (locationAge<60) {
NSLog(@"Found");
ctr=40;
homeloc.coordinate=newLocation.coordinate;
[self stopUpd];
[self reload];
} else {
NSLog(@"Lastupd");
lastloc.coordinate=newLocation.coordinate;
}
} else {
//enough tries, if not canceled choose lastloc.
if (ctr<40) {
NSLog(@"Last");
ctr=40;
homeloc.coordinate=lastloc.coordinate;
[self stopUpd];
[self reload];
}
}
}

//force stop updates. ctr prevents extra calls after stopUpdatingLocation.
//called after the timeout delay, if position found, cancel the timeout.
-(void)stopUpd {
[lm stopUpdatingLocation];
lm.delegate=nil;
if (ctr<15) {
ctr=40; //2 extra calls after stopupda... otherwise, now do nothing.
NSLog(@"Timeout");
homeloc.coordinate=lastloc.coordinate; //@need "copy"?
[self reload];
} else {
ctr=40; //2 extra calls after stopupda... otherwise, now do nothing.
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(stopUpd) object:nil];
}
}

// "couldn't get userlocation" handler. I also cancel like this on connectionDidFailWithError.
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
NSLog(@"Location failed: %@",[error localizedDescription]);
ctr=0;
[self stopUpd];
}

其中 ctr 是为了防止 2 个额外的调用,laSTLoc 是最后已知的可靠位置,homeloc 是用于通过[自重新加载](线程)加载特定于位置的数据的电话位置。

常数 15 和 60 是来自实际测试的安全值; gpstimeout 设置为 30。如果您在模拟器中工作很多,您可能需要将其设置为 3 秒之类的短值,因为您将得到的只是陈旧但相对可用的位置,并且在超时之前不会再有更多位置。

编辑:如果您可以优化我的代码或指出遗漏,我会将其标记为答案,我讨厌将自己的代码标记为答案。

关于iphone - 强制位置更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7078467/

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