locationupdate方法不必要地调用iPhone
我正在使用coreLocation制作我的应用程序。下面是我的类,它向代理类发送位置更新,代理类接收位置坐标并使用它们 问题是,当我第一次进入主视图(使用位置坐标的类是uiTableView,第二个是主视图旁边的类)时,它不会调用didUpdateToLocation,这很好,因为视图尚未加载 然后,当它的视图加载时,它会被调用两次,应该被调用一次。然后,当我再次处理我的应用程序n go to home view并按下iPhone的中央按钮时,该应用程序会转到后台,然后当我在home view Location上启动它时,会再次调用DidUpload方法4次,尽管尚未加载实现它的视图。我不想要这个,因为它使系统繁忙。请建议locationupdate方法不必要地调用iPhone,iphone,core-location,Iphone,Core Location,我正在使用coreLocation制作我的应用程序。下面是我的类,它向代理类发送位置更新,代理类接收位置坐标并使用它们 问题是,当我第一次进入主视图(使用位置坐标的类是uiTableView,第二个是主视图旁边的类)时,它不会调用didUpdateToLocation,这很好,因为视图尚未加载 然后,当它的视图加载时,它会被调用两次,应该被调用一次。然后,当我再次处理我的应用程序n go to home view并按下iPhone的中央按钮时,该应用程序会转到后台,然后当我在home view
- (id)init {
self = [super init];
if(self != nil) {
self.locMgr = [[[CLLocationManager alloc] init] autorelease]; // Create new instance of locMgr
self.locMgr.delegate = self; // Set the delegate as self.
self.locMgr.desiredAccuracy=kCLLocationAccuracyThreeKilometers;//check
self.locMgr.distanceFilter=500;//check in meters
[self.locMgr startUpdatingLocation];
}
return self;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
if([self.delegate conformsToProtocol:@protocol(CoreLocationControllerDelegate)]) { // Check if the class assigning itself as the delegate conforms to our protocol. If not, the message will go nowhere. Not good.
NSLog(@"core");
[self.delegate locationUpdate:newLocation];
}
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
if([self.delegate conformsToProtocol:@protocol(CoreLocationControllerDelegate)]) { // Check if the class assigning itself as the delegate conforms to our protocol. If not, the message will go nowhere. Not good.
if (error.code==kCLErrorDenied) {
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Permission Denied" message:@"Please allow location to retrieve location" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil , nil];
[alert show];
[alert release];
[locMgr stopUpdatingLocation];
[locMgr release];
locMgr=nil;
}
[self.delegate locationError:error];
}
}
- (void)dealloc {
[self.locMgr release];
[super dealloc];
}
@end
这很正常,如果每次的精度不同,它会被调用两次 检查
newLocation
的HorizontalAccurance
属性,对于每个调用,第二个调用可能比第一个调用在一次近似时发送的调用更精确(即使您使用KCLLOCATIONACURACYTHREEKMENTS
以获得所需的精度)
这与在谷歌地图上显示你的位置的“地图”应用程序完全相同:你首先有一个蓝色的大圆圈,上面有一个非常近似的位置,然后很快找到更准确的位置。您可以尝试将消息
startMonitoringSignificantLocationChanges
发送到您的CLLocationManager
,而不是startUpdatingLocation
?好的。但为什么在应用程序进入后台后,通过点击中间的黑色按钮,然后启动agian,它会被调用4次呢?你们解决了吗?在我的应用程序中,它在一秒钟内也被调用了4次。使用startMonitoringSignificantLocationChanges
而不是startUpdatingLocation
会导致CLLocationManager
类不经常发送更新,并且需要在更新之间进行更重要的位置更改。正如@AliSoftware所指出的,这可能会也可能不会阻止您遇到的问题。