다음 코드를 작성하여 현재 위치를 찾습니다. 아래의 lat2
및 logg2
을 NSLOg
에 인쇄 할 때 잘 작동하고 현재 위치를 인쇄하지만 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];
}
thnx.i 보관 유지 및 문제 해결 – nehal
우수,이 대답을 수락 할 수 있습니까? –