我寫了下面的代碼來查找當前位置。 它運行良好,打印當前位置時,我打印lat2
和logg2
下面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];
}
thnx.i添加保留和問題得到解決 – nehal 2011-03-22 12:45:33
非常好,你能接受這個答案嗎? – 2011-03-22 12:47:44