2013-05-08 20 views
0

我想從我的應用程序,它工作正常,一次用戶GPS座標。當視圖被調用時,我運行代碼並獲取位置 - 很好 - 但是當我回到視圖時,我需要NEW座標,但是我只能看到像手機中的舊座標那樣的舊座標。stopUpdatingLocation後不能獲得新的位置 - IOS

這裏是我的代碼:

#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每秒發送一次新的座標。每次觀看時我只需要一次。

任何人都有線索? 在此先感謝

回答

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; 
} 
+0

感謝您的答覆,但它不工作100%,它更新每個其他時間,當我剛去退一萬步,並將其轉發給(?)該視圖再次不更新 – PaperThick 2013-05-08 14:33:18

+0

@PaperThick現在嘗試,我編輯。它會在每次出現視圖時更新位置,並在它獲取後立即停止。我明白你的意思,這是對的嗎? – 2013-05-08 14:39:19

+0

沒有抱歉仍然是同樣的問題。我用一些額外的代碼更新了我的問題,但我很確定它不重要 – PaperThick 2013-05-08 14:45:39

相關問題