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毎秒新しい座標を送信したくありません。視聴時間ごとに 1 回だけ必要です。

誰でも手がかりを得ましたか?前もって感謝します

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 に答える