I have written the following code to find the current location.
It works well and prints the current location when I print lat2
and logg2
below in NSLOg
, but it is not ass开发者_运维问答igning value to lables. When I print label the value is null.
And I want to access lat2 and logg2 values outside the method didUpdateToLocation. I am not able to access these values outside the didUpdateToLocation method even though I have declared logg2 and lat2 globally. Outside this method it gives null value. How can I do that? What is problem here? Is there any sample code or tutorial fot that?
- (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];
}
Basic memory management:
lat2 = [NSString stringWithFormat: @"%f", loc.latitude];
logg2= [NSString stringWithFormat: @"%f", loc.longitude];
These strings are destroyed (deallocated) almost right after you create them. Read up on memory management guidelines. Don't use globals either. If you must, this should fix your strings disappearing:
[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];
Oh, and did you remember to connect the outlets to your labels in Interface Builder?
Use this to show your latitude and longitude in labels.
labellat.text = [NSString stringWithFormat: @"%.6f", loc.latitude];
labellog.text= [NSString stringWithFormat: @"%.6f", loc.longitude];
Please retain the location coordinate to get the values if you want call this from any other places, otherwise it may autorelase.
You can make use of
labellat.text = [NSString stringWithFormat: @"%@", lat2];
labellog.text= [NSString stringWithFormat: @"%@", logg2];
But I think it's better to store the latitude and longitude values as a float itself in an instance variable and thereby getting access to it easily. If you want to show it in a label, use the method stringWithFormat:
just as you did.
UPDATE:
The tutorial Hello There: A CoreLocation Tutorial may help you.
精彩评论