【问题标题】:Cant get new location after stopUpdatingLocation - IOS停止更新位置后无法获取新位置 - IOS
【发布时间】:2013-05-08 14:20:12
【问题描述】:

我正在尝试从我的应用程序中获取用户的 GPS 坐标,该应用程序运行良好,一次。当视图被调用时,我运行代码并获取位置 - 很好 - 但是当我回到视图时,我想要新的坐标,但我只得到旧的坐标,就像它们在手机中一样。

这是我的代码:

#import "PostPictureViewController.h"

@interface PostPictureViewController ()

@end

@implementation PostPictureViewController

{
    CLLocationManager *locationManager;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];           
}

-(void) viewDidAppear:(BOOL)animated{
    locationManager = [[CLLocationManager alloc] init];
    [self getLocation];
}

-(void)getLocation{
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    [locationManager startUpdatingLocation];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"didFailWithError: %@", error);
    UIAlertView *errorAlert = [[UIAlertView alloc]
                               initWithTitle:@"Fel" message:@"Error" 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([NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude]);
        NSLog([NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude]);
    }
    // Stop Location Manager
    [locationManager stopUpdatingLocation]; //This is screwing it up
    locationManager = nil;
}

.h

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@interface PostPictureViewController : UIViewController<CLLocationManagerDelegate>

@end

我认为我的问题是我第一次获得坐标后停止 locationManager 的[locationManager stopUpdatingLocation]; 行。我试过没有这条线,然后它可以工作 - 更新坐标 - 但我不希望 locationmanager 每秒向我发送新坐标。每个观看时间我只需要它们一次。

有人知道吗? 提前致谢

【问题讨论】:

    标签: iphone ios objective-c gps coordinates


    【解决方案1】:

    这样试试:

    - (void)viewDidLoad
    {
     [super viewDidLoad];   
     locationManager = [[CLLocationManager alloc] init];
     [self getLocation];
    }
    
    -(void) viewWillAppear:(BOOL)animated{
     [locationManager startUpdatingLocation];
    }
    
    -(void)getLocation{
     locationManager.delegate = self;
     locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    }
    
    - (void)didReceiveMemoryWarning
    {
     [super didReceiveMemoryWarning];
     // Dispose of any resources that can be recreated.
    }
    
    #pragma mark - CLLocationManagerDelegate
    
    - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
    {
     NSLog(@"didFailWithError: %@", error);
     UIAlertView *errorAlert = [[UIAlertView alloc]
                               initWithTitle:@"Fel" message:@"Error" 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([NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude]);
        NSLog([NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude]);
     }
     // Stop Location Manager
     [locationManager stopUpdatingLocation]; //This is screwing it up
    }
    
    - (void)viewDidUnload
    {
     locationManager = nil;
    }
    

    【讨论】:

    • 感谢您的回复,但它不能 100% 工作,它会每隔一段时间更新一次(?),当我返回一步并且它们再次转发到视图时它不会更新
    • @PaperThick 现在试试,我编辑了。每次视图出现时它都会更新位置,并在获得它后立即停止。我知道你想要那个,对吗?
    • 不抱歉,还是同样的问题。我用一些额外的代码更新了我的问题,但我很确定这并不重要
    • @PaperThick 我现在不知道您要做什么,但我测试了我的代码并且它可以正常工作。基本上我将该代码放在一个名为 PostPictureViewController 的视图控制器中。因此,当我显示视图控制器时,位置会更新,然后停止。之后,如果我从 PostPictureViewController 推送另一个视图控制器 SuccessPictureViewController,然后返回 PostPictureViewController,那么位置会再次更新。那有什么问题?也许你必须更好地解释你想要做什么。
    • 当然,如果您的视图控制器 PostPictureViewController 总是在屏幕上,那么代码将无法工作,因为 viewWillAppear 再也不会被调用。所以在这种情况下你需要改变它。
    猜你喜欢
    • 2017-12-16
    • 2019-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 2017-05-30
    • 1970-01-01
    相关资源
    最近更新 更多