2011-03-22 6 views
1

다음 코드를 작성하여 현재 위치를 찾습니다. 아래의 lat2logg2NSLOg에 인쇄 할 때 잘 작동하고 현재 위치를 인쇄하지만 lables에 값을 할당하지 않습니다. 레이블을 인쇄 할 때 값은 null입니다.iPhone에서 현재 위치를 찾는 방법은 무엇입니까?

그리고 didUpdateToLocation 메서드 외부에서 lat2 및 logg2 값에 액세스하려고합니다. 전역으로 logg2 및 lat2를 선언했지만 didUpdateToLocation 메서드 외부에서 이러한 값에 액세스 할 수 없습니다. 이 메소드 밖에서는 null 값을 제공합니다. 어떻게해야합니까? 여기에 무슨 문제가 있습니까? 거기에 어떤 샘플 코드 또는 튜토리얼 fot가 있습니까?

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

오, 당신은 인터페이스 빌더에서 레이블에 콘센트를 연결하는 것을 잊지 않았다?

+0

thnx.i 보관 유지 및 문제 해결 – nehal

+3

우수,이 대답을 수락 할 수 있습니까? –

0

당신은

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

를 사용할 수있다 그러나 나는 쉽게 그것에 위도와 경도 인스턴스 변수의 float 자체 값함으로써 얻는 액세스를 저장하기 위해 더 나은 것 같아요. 레이블에 표시하려면 stringWithFormat: 메서드를 사용하십시오.

UPDATE :

Hello There: A CoreLocation Tutorial 당신을 도울 수 자습서.

0

라벨에 위도와 경도를 표시하는 데 사용합니다.

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

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

그렇지 않으면 autorelase 수, 위치가 어떤 다른 장소에서이 전화를하려면 값을 얻을 좌표 보관하십시오.

관련 문제