如何在 iPhone 中查找当前位置?
我编写了以下代码来查找当前位置。 当我在 NSLOg
中打印下面的 lat2
和 logg2
时,它运行良好并打印当前位置,但它没有为标签赋值。当我打印标签时,该值为空。
我想在 didUpdateToLocation 方法之外访问 lat2 和 logg2 值。 即使我无法在 didUpdateToLocation 方法之外访问这些值 我已在全球范围内声明了 logg2 和 lat2 。在此方法之外,它给出空值。我怎样才能做到这一点?这里有什么问题?有相关的示例代码或教程吗?
- (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];
}
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 assigning 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];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
基本内存管理:
这些字符串几乎在创建后立即被销毁(释放)。阅读内存管理指南。也不要使用全局变量。如果必须的话,这应该可以修复字符串消失的问题:
哦,您是否记得将插座连接到 Interface Builder 中的标签?
Basic memory management:
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:
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.
Please retain the location coordinate to get the values if you want call this from any other places, otherwise it may autorelase.
您可以利用
但我认为最好将纬度和经度值作为浮点数本身存储在实例变量中,从而轻松访问它。如果您想在标签中显示它,请像您一样使用方法
stringWithFormat:
。更新:
教程你好:A CoreLocation 教程可能会对您有所帮助。
You can make use of
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.