Objective C,iOS 7 - CLLocationManager:didUpdateLocations只被调用一次 [英] Objective C, iOS 7 - CLLocationManager: didUpdateLocations is being called only once

查看:130
本文介绍了Objective C,iOS 7 - CLLocationManager:didUpdateLocations只被调用一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:好吧,我只是注意到,问题是独家的iOS 7.我仍然无法解决它,但是,所以我认为我会尝试与下面的sugestions。感谢所有!



嗨!我正在编写一个导航器应用程序,并且我需要在任何可能的时候更新用户位置。我有两个使用CLLocation manager的View Controller。在他们两人中,我都添加了这一行:

  #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:@错误消息:@无法获得您的位置委托:无cancelButtonTitle:@OKotherButtonTitles:nil];
[errorAlert show]; (CLLocationManager *)经理didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@B

$ b - 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);
}
}

第二个视图。正如你所看到的,我用didUpdateLocations取代了旧的didUpdateToLocation,作为一个绝望的尝试。

  #pragma mark  -  CLLocationManagerDelegate 

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@didFailWithError:%@,error);
UIAlertView * errorAlert = [[UIAlertView alloc]
initWithTitle:@错误消息:@无法获得您的位置委托:无cancelButtonTitle:@OKotherButtonTitles:nil];
[errorAlert show]; (CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog($)
/ * - (void)locationManager: (@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:% @,[位置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];如果([mapView getNearestDestinyDistance:currentLocation]< 150.0){
NSLog(@Hay que eliminar el primer destino del MapView);
}

if
mapView.destinoActual ++; ([mapView getNearestPointDistance:currentLocation]> 200.0){
NSLog(@Hay que recalcular la ruta al destinoActual);
}

if
SpinnerView * spinner = [Spin​​nerView loadSpinnerIntoView:self.view];
spinner.tag = 98;
while(![mapView recalculateRoutesTo:currentLocation]){
// dohinhin ...
}
[spinner removeSpinner];




// [locationManager stopUpdatingLocation];

$ / code>

正如您所看到的那样,

  // [locationManager stopUpdatingLocation]; 

会被评论。那么,这个问题让我感到疯狂的是,上面的代码在第一个视图控制器中起到了魅力的作用,并按我的预期定期更新。但在第二种观点中,它只是第一次运作,并且不会再更新。我在不同的时间对这些方法进行了编程,所以我不记得我是否对第一种观点做了些什么,但是我没有做到。但他们在同一个应用程序,所以我认为这是没有问题的库或权限。欢迎提供任何帮助或提示,谢谢!

发现解决方案这里。当我以这种方式启动locationManager时,locationManager会工作:

  if(self){
locationManager = [[CLLocationManager alloc]在里面];
[locationManager setDelegate:self];
[locationManager setDistanceFilter:kCLHeadingFilterNone];
//将所需的精确度更改为kCLLocationAccuracyBest
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
// SOLUTION:set setPausesLocationUpdatesAutomatically为NO
[locationManager setPausesLocationUpdatesAutomatically:NO];
[locationManager startUpdatingLocation];
}

无论如何,我会像Rob建议的那样表现工作。感谢所有人的帮助!


EDIT: Ok, I just have noticed that the problem is exclusive of iOS 7. I'm still being unable to solve it, however, so I suppouse I'll try with the sugestions bellow. Thanks to all!

¡Hi! I'm programming a navigator app, and I need to update the user position whenever is possible. I have two View Controllers that use CLLocation manager. In both of them I've added this line:

#import <CoreLocation/CoreLocation.h>

And then added the to the interface declaration, and I'm setting this as a property in the .h file, and synthetizing afterwards:

@property (strong, nonatomic) CLLocationManager *locationManager;

After that, I'm launching de locationManager in the viewDidLoad, this way:

if(self){
    locationManager = [[CLLocationManager alloc] init];
    //locationManager.delegate = self;
    [locationManager setDelegate:self];
    [locationManager setDistanceFilter:kCLHeadingFilterNone];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBestForNavigation];
    [locationManager startUpdatingLocation];
}

And here are my delegate methods.

For the first View:

#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);
    }
}

For the second view. As you can see, I replaced the old didUpdateToLocation with didUpdateLocations, as a desperate try.

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

As you can see, the line

//[locationManager stopUpdatingLocation];

is commented. Well, the problem is driving me crazy is that the code above works as a charm in the first view controller, and update periodically as I expected. But in the second view, it works only the first time, and never updates again. I programmed these methods in different times, so I can't remember if I did something to the first view I didn't to the second. But they are in the same app, so I assume there's no problem with libraries or permissions. Any help or hint is welcome, thanks!

解决方案

Found the solution here. The locationManager works when I launch it this way:

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

I'll work anyway in performance like Rob suggested. Thanks to all for the help!

这篇关于Objective C,iOS 7 - CLLocationManager:didUpdateLocations只被调用一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆