时不时地获取位置坐标的问题
有时在我的应用程序上获取位置坐标时会出现问题。
我一直在测试我的应用程序的位置坐标,从模拟器和坐在我家里的iPhone(这不仅仅是我的家,我在不同的位置(室外)以及非常好的网络连接进行了测试),我看到了这个有线行为,我现在有正确的坐标,然后我将应用程序发送到后台并将其带回来,我会得到正确的位置坐标,如果我这样做 8-10 次(即将其发送到后台并将其带回)它到前台)偶尔之后该应用程序来自后台,我无法获取位置坐标,此时获取位置坐标的唯一方法是杀死该应用程序,然后重新开始。所以我确信出了什么问题,但我不确定是什么。
这就是我正在做的
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
//if the time interval returned from core location is more than 30 seconds we ignore it because it might be from an old session
if ( abs([newLocation.timestamp timeIntervalSinceDate: [NSDate date]]) < 30) {
if(newLocation.coordinate.latitude != previousLocation.coordinate.latitude && newLocation.coordinate.longitude != previousLocation.coordinate.longitude){
if(newLocation.horizontalAccuracy <= 100){
[self.locationManager stopUpdatingLocation];
[self.locationManager startMonitoringSignificantLocationChanges];
}
else{
[self.locationManager startUpdatingLocation];
}
所以基本上我只在新位置不早于 30 秒时才采用它,并且它与我在本地存储的先前位置不同并且水平精度小于 100 米。当我在调试器中运行它时,我观察到的是,我到达第一个 if 条件 3-4 次,如果失败,则此后不会出现,这意味着 didUpdateToLocation 根本不会被调用。
一旦坐标满足我的所有标准,我就会停止更新位置并开始监视重要位置更改。
我在 else 块中执行 startUpdatingLocation 的原因是。
例如,如果 didUpdateToLocation 由于 startMonitoringSignificantLocationChanges 被调用,我想在那之后获得准确的位置,所以每次我没有得到我正在寻找的正确位置时,我都会执行 startUpdatingLocation,因为我相信执行多个 startUpdatingLocation 不会造成伤害任何事物。
如果我的思维过程或代码逻辑有问题,请告诉我。
time to time there is an issue with geting the location coordinate on my app.
I have been testing my app for location coordinate from the simulator and the iphone sitting at my home (it is not just my home, i tested it in different location (outdoor) as well with a very good network connectivity), and i see this wired behavior, i have the right co ordinate at the moment and then i send the app to the background and bring it back i get the right location co ordinate, and if i do it 8-10 times (i.e sending it to background and bringing it to foreground) once in a while after the app comes from the background i cannot get location co ordinate, the only way to get the location co ordinate at this moment is to kill the app and then start fresh. So i am sure some thing is going wrong but I am not sure what is it.
This is what I am doing
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
//if the time interval returned from core location is more than 30 seconds we ignore it because it might be from an old session
if ( abs([newLocation.timestamp timeIntervalSinceDate: [NSDate date]]) < 30) {
if(newLocation.coordinate.latitude != previousLocation.coordinate.latitude && newLocation.coordinate.longitude != previousLocation.coordinate.longitude){
if(newLocation.horizontalAccuracy <= 100){
[self.locationManager stopUpdatingLocation];
[self.locationManager startMonitoringSignificantLocationChanges];
}
else{
[self.locationManager startUpdatingLocation];
}
So basically i take the newLocation only if it is not older then 30 sec, and it is not same as previous location that i have stored locally and the horizontal accuracy is less than 100 meter. When i run it in debugger what i am observing is i get to the first if condition 3-4 times and if it fails it doesn't come after that, which means didUpdateToLocation doesn't get called at all.
Once the co ordinate meet all my criteria I do stopupdatinglocation and do startMonitoringSignificantLocationChanges.
The reason i am doing startUpdatingLocation in my else block is.
For example if didUpdateToLocation got called due to the startMonitoringSignificantLocationChanges, i want to get the accurate location after that so i am doing startUpdatingLocation every time i don't get the right location that i am looking for as i believe doing multiple startUpdatingLocation doesn't harm anything.
Let me know if there is something wrong in my thought process or in the code logic.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我要检查的第一件事是你的 info.plist。您必须包含关键的必需后台模式 ->应用程序注册位置更新。如果没有这个,您将不会在后台收到位置更新(除了显着的位置变化和区域监控)。
在主要的 else 中,您调用 startUpdatingLocation。虽然我不相信这会造成任何伤害,但除非你用 stopUpdatingLocation 来平衡它,否则我不相信它会做任何事情。文档说:
但无论哪种方式,我认为您都不需要告诉它再次 startUpdating,当 distanceFilter 时它会自行继续属性超出或硬件收集更准确的位置读数。
我没有看到你的第二个 if 的结尾 } 。
The first thing I would check is your info.plist. You have to include the key Required Background Modes -> App registers for location updates. Without this you won't receive location updates (besides significant location changes and region monitoring) while in the background.
In the main else, you call startUpdatingLocation. While I don't believe this hurts anything, unless you are balancing it with a stopUpdatingLocation I don't believe it will do anything. Documentation says:
But either way, I don't think you need to tell it to startUpdating again, it will continue on it's own when the distanceFilter property is exceeded or the hardware gathers a more accurate location reading.
I don't see a closing } for your second if.