0

我正在尝试从我的应用程序中获取用户 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每秒向我发送新坐标。每个观看时间我只需要它们一次。

有人有线索吗?提前致谢

4

1 回答 1

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;
}
于 2013-05-08T14:27:42.360 回答