ios - locationManager返回0.0000,错误在哪里?

标签 ios objective-c cllocationmanager xcode4.6.3

我正在编写一个应用程序,除其他外,它具有显示地址和用户所在位置的经度/纬度的功能。

我已经设置了 locationManager,并希望正确地将 float 转换为字符串,但是当我测试应用程序时,它仍然显示为 0.00000。

问题出在locationManager上吗?或者信息是否在途中丢失了?我这里有我的代码:

谢谢

    //
//  StartVC.m
//

#import "StartVC.h"

@interface StartVC () {
/*    IBOutlet UILabel * _Gatenavn;
    IBOutlet UILabel * _Gatenr;
    IBOutlet UILabel * _Postnr;
    IBOutlet UILabel * _Poststed; */
    IBOutlet UILabel * _Lengdegrad;
    IBOutlet UILabel * _Breddegrad;
    float Breddegrad;
    float Lengdegrad;
}

@property (assign, nonatomic) CLLocationDistance distanceFilter;
@property(assign, nonatomic) CLLocationAccuracy desiredAccuracy;


@end


@implementation StartVC

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _Breddegrad.text = [NSString stringWithFormat:@"%1.6f", Breddegrad];
    _Lengdegrad.text = [NSString stringWithFormat:@"%1.6f", Lengdegrad];
    
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)startSignificantChangeUpdates
{
    CLLocationManager *locationManager = [[CLLocationManager alloc] init];
    
    if (nil == locationManager)
        locationManager = [[CLLocationManager alloc] init];
    
    locationManager.delegate = self;
    [locationManager startMonitoringSignificantLocationChanges];
}


- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray *)locations {
    // If it's a relatively recent event, turn off updates to save power
    CLLocation* location = [locations lastObject];
    NSDate* eventDate = location.timestamp;
    NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
    if (abs(howRecent) < 15.0) {
        // If the event is recent, do something with it.
        Breddegrad = location.coordinate.latitude;
        Lengdegrad = location.coordinate.longitude;
    }
}


@end

最佳答案

您没有更新标签。您应该将以下内容移至另一个方法并在 locationManager:didUpdateLocations: 中调用它。

_Breddegrad.text = [NSString stringWithFormat:@"%1.6f", Breddegrad];
_Lengdegrad.text = [NSString stringWithFormat:@"%1.6f", Lengdegrad];

关于ios - locationManager返回0.0000,错误在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18498664/

相关文章:

ios - 如何在不同的 View Controller 中分离模块

ios - 使用 ARC 进行内存分配 - 没有泄漏,但会慢慢堆积内存

ios - 如何在文本字段中仅接受一个小数点值?

ios - NSMutableArray 对副本的更改也会导致父 NSMutableArray 的更改

ios - 单个 UILabel 中的粗体和非粗体文本?

ios - iOS最小化应用程序时,如何每隔特定N天推送一次本地通知?

ios - 停止和启动 MonitoringForRegion 时监控区域失败 - iOS 7.1

ios - 尝试仅在启用位置服务时运行代码导致无限循环

ios - xcode 无法识别@property,但它是正确的

Swift,MKPolyline 如何在坐标点之间创建折线