在stopUpdatingLocation - IOS之后无法获取新位置

时间:2013-05-08 14:20:13

标签: iphone ios objective-c gps coordinates

我正试图从我的应用程序中获取用户的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 stopUpdatingLocation];行,我在第一次获取坐标后停止了locationManager。我试过没有这条线然后它工作 - 更新坐标 - 但我不希望locationmanager每秒向我发送新的坐标。每个观看时间我只需要一次。

任何人都有线索? 提前致谢

1 个答案:

答案 0 :(得分:2)

尝试这种方式:

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