Iphone 停止更新位置-IOS后无法获取新位置

Iphone 停止更新位置-IOS后无法获取新位置,iphone,ios,objective-c,gps,coordinates,Iphone,Ios,Objective C,Gps,Coordinates,我正在尝试从我的应用程序中获取一个用户的GPS坐标,它可以正常工作一次。当视图被调用时,我运行代码并得到位置-很好-但是当我回到视图时,我想要新的坐标,但我只得到旧的坐标,就像它们在电话中被切割一样 这是我的密码: #import "PostPictureViewController.h" @interface PostPictureViewController () @end @implementation PostPictureViewController { CLLocat

我正在尝试从我的应用程序中获取一个用户的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

#导入
#进口
@接口PostPictureViewController:UIViewController
@结束
我想我的问题是
[locationManager StopUpdateingLocation]第一次获取坐标后停止locationManager的行。我试过不用这条线,然后它就可以工作了-更新坐标-但我不希望
locationmanager
每秒都给我发送新的坐标。我每次只需要一次

有人有线索吗? 提前感谢

请尝试以下方法:

- (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%工作,它每隔一段时间就会更新一次(?),而当我只返回一步,他们就再次转到视图时,它就不能工作了update@PaperThick现在试试,我编辑过。每次视图出现时,它都会更新位置,并在获取视图后立即停止。我知道你想要那个,对吗?不抱歉还是一样的问题。我用一些额外的代码更新了我的问题,但我很确定它不是important@PaperThick我现在不知道你想做什么,但我测试了我的代码,它工作正常。基本上我把代码放在一个叫做PostPictureViewController的视图控制器中。因此,当我显示视图控制器时,位置会更新,然后停止。之后,如果我从PostPictureViewController成功推送另一个视图控制器PictureViewController,然后返回PostPictureViewController,那么位置将再次更新。怎么了?也许你必须更好地解释你想做什么。当然,如果你的视图控制器PostPictureViewController始终在屏幕上,那么代码将无法工作,因为
视图将出现
不再被调用。所以在这种情况下,你需要改变它。
- (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;
}