如何在 iPhone 中查找当前位置?

发布于 2024-10-24 19:26:47 字数 2154 浏览 4 评论 0原文

我编写了以下代码来查找当前位置。 当我在 NSLOg 中打印下面的 lat2logg2 时,它运行良好并打印当前位置,但它没有为标签赋值。当我打印标签时,该值为空。

我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

塔塔猫 2024-10-31 19:26:47

基本内存管理:

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

哦,您是否记得将插座连接到 Interface Builder 中的标签?

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?

书信已泛黄 2024-10-31 19:26:47

使用它在标签中显示您的纬度和经度。

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

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

如果您想从任何其他地方调用它,请保留位置坐标以获取值,否则它可能会自动释放。

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.

方圜几里 2024-10-31 19:26:47

您可以利用

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

但我认为最好将纬度和经度值作为浮点数本身存储在实例变量中,从而轻松访问它。如果您想在标签中显示它,请像您一样使用方法stringWithFormat:

更新:

教程你好:A CoreLocation 教程可能会对您有所帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文