[英]CLLocationManager didUpdateLocations not being called iOS 8 with plist entries
[英]Objective C, iOS 7 - CLLocationManager: didUpdateLocations is being called only once
編輯:好的,我只是注意到該問題不包括iOS7。但是,我仍然無法解決它,因此,我支持我嘗試以下問題。 謝謝大家!
¡嗨! 我正在編程一個導航器應用程序,因此我需要盡可能地更新用戶位置。 我有兩個使用CLLocation管理器的視圖控制器。 在它們兩個中,我都添加了以下行:
#import <CoreLocation/CoreLocation.h>
然后將添加到接口聲明中,然后將其設置為.h文件中的屬性,然后進行合成:
@property (strong, nonatomic) CLLocationManager *locationManager;
之后,我將以這種方式在viewDidLoad中啟動de locationManager:
if(self){
locationManager = [[CLLocationManager alloc] init];
//locationManager.delegate = self;
[locationManager setDelegate:self];
[locationManager setDistanceFilter:kCLHeadingFilterNone];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBestForNavigation];
[locationManager startUpdatingLocation];
}
這是我的委托方法。
對於第一個視圖:
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError: %@", error);
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"didUpdateToLocation: %@", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
homeLatitude = [[NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude] doubleValue];
homeLongitude = [[NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude] doubleValue];
NSLog(@"Updated! -> hl = %f, hlg = %f", homeLatitude, homeLongitude);
}
}
對於第二種觀點。 如您所見,我拼命嘗試將舊的didUpdateToLocation替換為didUpdateLocations。
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError: %@", error);
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
}
/*- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"didUpdateToLocation: %@", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
NSLog(@"Tarzan boy");
_testLabel.text = [NSString stringWithFormat:@"Oh DAMN!!!!"];
}
}*/
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
NSLog(@"didUpdateToLocation: %@", [locations lastObject]);
CLLocation *currentLocation = [locations lastObject];
if (currentLocation != nil) {
currentLatitude = [[NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude] doubleValue];
currentLongitude = [[NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude] doubleValue];
NSLog(@"Updated! (MapWithRoutesViewController) -> hl = %f, hlg = %f", currentLatitude, currentLongitude);
_testLabel.text = [NSString stringWithFormat:@"Update! -> hl = %f, hlg = %f", currentLatitude, currentLongitude];
//Aquí es donde borramos el lugar al que llegaron.
if(mapView){
if(followMe){
CLLocationCoordinate2D *c = &((CLLocationCoordinate2D){.latitude = currentLatitude, .longitude = currentLongitude});
[mapView.mapView as_setCenterCoordinate:*c zoomLevel:32 animated:NO];
_ourStepper.value = [mapView.mapView as_zoomLevel];
}
if([mapView getNearestDestinyDistance:currentLocation] < 150.0){
NSLog(@"Hay que eliminar el primer destino del MapView");
mapView.destinoActual++;
}
if([mapView getNearestPointDistance:currentLocation] > 200.0){
NSLog(@"Hay que recalcular la ruta al destinoActual");
SpinnerView *spinner = [SpinnerView loadSpinnerIntoView:self.view];
spinner.tag = 98;
while (![mapView recalculateRoutesTo:currentLocation]) {
//do nothin...
}
[spinner removeSpinner];
}
}
}
//[locationManager stopUpdatingLocation];
}
如您所見,該行
//[locationManager stopUpdatingLocation];
被評論。 嗯,問題使我發瘋了,因為上面的代碼在第一個視圖控制器中起了魅力,並按我的預期定期更新。 但是在第二種視圖中,它只能在第一次使用,而不會再更新。 我在不同的時間對這些方法進行了編程,所以我不記得我是否對第一個視圖做了什么而對第二個視圖沒有做。 但是它們在同一個應用程序中,因此我認為庫或權限沒有問題。 歡迎任何幫助或提示,謝謝!
首先,如果要進行導航,則應該注冊以獲得重大更改通知。 在此處閱讀有關該文檔的文檔。 效率更高。 不使用時必須將其關閉,但這要好得多。 對它的支持可以一直追溯到iOS4。可以肯定的是,這是99%的用戶。
我在應用程序中遇到了類似情況,並實現了一個處理所有位置更新的單例,然后將NSNotificaitons觸發到前台線程。
這可能不是您要尋找的答案,但我知道它可以解決我的問題。
在這里找到解決方案。 當我以這種方式啟動時,locationManager可以工作:
if(self){
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDistanceFilter:kCLHeadingFilterNone];
//change the desired accuracy to kCLLocationAccuracyBest
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
//SOLUTION: set setPausesLocationUpdatesAutomatically to NO
[locationManager setPausesLocationUpdatesAutomatically:NO];
[locationManager startUpdatingLocation];
}
無論如何,我都會按照Rob的建議工作。 感謝大家的幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.