【问题标题】:How to find the current location in iPhone?如何在 iPhone 中找到当前位置?
【发布时间】:2011-03-22 12:18:04
【问题描述】:

我编写了以下代码来查找当前位置。 当我在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];
}

【问题讨论】:

    标签: iphone location core-location latitude-longitude


    【解决方案1】:

    基本内存管理:

    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 中将插座连接到您的标签吗?

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

        【解决方案3】:

        你可以利用

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

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

        更新:

        教程Hello There: A CoreLocation Tutorial可能对你有所帮助。

        【讨论】:

          猜你喜欢
          • 2012-08-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-03-28
          • 1970-01-01
          相关资源
          最近更新 更多