2011-03-22 72 views
1

我寫了下面的代碼來查找當前位置。 它運行良好,打印當前位置時,我打印lat2logg2下面NSLOg,但它不分配價值標籤。當我打印標籤時,該值爲空。如何在iPhone中查找當前位置?

我想訪問方法didUpdateToLocation之外的lat2和logg2值。 即使 我已經在全局聲明瞭logg2和lat2,但我無法在didUpdateToLocation方法之外訪問這些值。在這個方法之外,它給出了空值。我怎樣才能做到這一點?這裏有什麼問題?有沒有示例代碼或教程嗎?

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization. 
    } 
    return self; 
} 

-(void)awakeFromNib { 
    [self update]; 
} 

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    if (wasFound) return; 
    wasFound = YES; 

    CLLocationCoordinate2D loc = [newLocation coordinate]; 

    lat2 = [NSString stringWithFormat: @"%f", loc.latitude]; 
    logg2= [NSString stringWithFormat: @"%f", loc.longitude]; 
    NSLog(@"latitude is %@",lat2); 
    NSLog(@"longitude is %@",logg2); 
    NSLog(@"*******This is the login view controller did update locationmethod of loginview controller."); 
    NSLog(@"latitude is %f",[lat2 floatValue]); 
    NSLog(@"longitude is %f",[logg2 floatValue]); 
    labellat.text = [NSString stringWithFormat: @"%f", loc.latitude]; 
    labellog.text= [NSString stringWithFormat: @"%f", loc.longitude]; 
    //NSLog(@"text of label is %@",labellat.text); 
    //NSLog(@"text of label is %@",labellog.text); 

    //lat=loc.latitude; 
    //log=loc.longitude; 
    //altitude.text = [NSString stringWithFormat: @"%f", newLocation.altitude]; 

    // NSString *mapUrl = [NSString stringWithFormat: @"http://maps.google.com/maps?q=%f,%f", loc.latitude, loc.longitude]; 
    // NSURL *url = [NSURL URLWithString:mapUrl]; 
    // [[UIApplication sharedApplication] openURL:url]; 
} 

- (IBAction)update { 
    locmanager = [[CLLocationManager alloc] init]; 
    [locmanager setDelegate:self]; 
    [locmanager setDesiredAccuracy:kCLLocationAccuracyBest]; 
    NSLog(@"*********This is the location update method of login view controller."); 
    [locmanager startUpdatingLocation]; 
} 

回答

1

基本的內存管理:

lat2 = [NSString stringWithFormat: @"%f", loc.latitude]; 
logg2= [NSString stringWithFormat: @"%f", loc.longitude]; 

這些字符串被破壞(解除分配)在創建後差不多吧。閱讀內存管理指南。不要使用全局變量。如果你一定要,這應該解決您的字符串消失:

[lat2 release]; 
[logg2 release]; 
lat2 = [[NSString stringWithFormat: @"%f", loc.latitude] retain]; // keep string alive until we release it 
logg2= [[NSString stringWithFormat: @"%f", loc.longitude] retain]; 

哦,你記得到出口連接到在Interface Builder標籤嗎?

+0

thnx.i添加保留和問題得到解決 – nehal 2011-03-22 12:45:33

+3

非常好,你能接受這個答案嗎? – 2011-03-22 12:47:44

0

您可以使用

labellat.text = [NSString stringWithFormat: @"%@", lat2]; 
labellog.text= [NSString stringWithFormat: @"%@", logg2]; 

但我認爲這是更好的緯度和經度值的浮動本身的實例變量,從而獲得訪問輕鬆存儲到它。如果您想將其顯示在標籤中,請使用方法stringWithFormat:,就像您一樣。

UPDATE:

教程Hello There: A CoreLocation Tutorial可以幫助你。

0

使用此選項可以在標籤中顯示您的緯度和經度。

labellat.text = [NSString stringWithFormat: @"%.6f", loc.latitude]; 

labellog.text= [NSString stringWithFormat: @"%.6f", loc.longitude]; 

如果您想從任何其他地方調用此值,請保留位置座標值,否則它可能會自動再生。