【问题标题】:Objective-C Location to label textObjective-C 标记文本的位置
【发布时间】:2014-01-30 21:16:56
【问题描述】:

我对 Objective C 开发还很陌生。

我正在尝试获取经度和纬度坐标并将其分配给标签。

但是,当我加载应用程序时,我的标签不会随坐标更新。

我确定我做错了一些非常基本的错误,但任何帮助将不胜感激!

.h:

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import "ViewController.h"

@interface StartCycleViewController : UIViewController <NSXMLParserDelegate, CLLocationManagerDelegate>
@property (weak, nonatomic) IBOutlet UILabel *longitudeLabel;
@property (weak, nonatomic) IBOutlet UILabel *latitudeLabel;
@property (nonatomic,strong) CLLocation *currentCycleLocation;
@property (nonatomic,strong) CLLocationManager *cycleLocation;

- (void) stopLocationManager;


@end

.m :

#import "StartCycleViewController.h"

@interface StartCycleViewController ()

@end

@implementation StartCycleViewController

@synthesize cycleLocation = _cycleLocation;
@synthesize currentCycleLocation = _currentCycleLocation;


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

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self startCycleLocation];
    // Do any additional setup after loading the view.
}

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


#pragma mark - startCycleLocation

- (void)startCycleLocation{

    if (!_cycleLocation){
        _cycleLocation = [[CLLocationManager alloc]init];
        _cycleLocation.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
        _cycleLocation.distanceFilter = 10;
        _cycleLocation.delegate = self;

    }
    [_cycleLocation startUpdatingLocation];

}

-(void) cycleLocation:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
         fromLocation:(CLLocation *)oldLocation {

    NSLog(@"didUpdateToLocation: %@", newLocation);
    CLLocation *currentLocation = newLocation;

    if (currentLocation != nil) {
        self.longitudeLabel.text = [NSString stringWithFormat:@"%.8f",          currentLocation.coordinate.longitude];
        self.latitudeLabel.text = [NSString stringWithFormat:@"%.8f",   currentLocation.coordinate.latitude];
    }

}


- (void) locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"%@",error);

    if ( [error code] != kCLErrorLocationUnknown ){
        [self stopLocationManager];
    }
}

- (void) stopLocationManager {
    [self.cycleLocation stopUpdatingLocation];
}

@end

【问题讨论】:

    标签: objective-c gps location


    【解决方案1】:

    我会将此作为评论留下,但我没有这样做的声誉。

    更新标签的那一行发生了什么?

    self.longitudeLabel.text = [NSString stringWithFormat:@"%.8f",      c    currentLocation.coordinate.longitude];
    

    为什么那里只有一个c?

    编辑:所以我实现了这个方法

    -(void) cycleLocation:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
    

    从未被调用。在挖掘之后,我发现方法应该是这样的:

    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
    

    用那个替换旧签名,它应该可以工作。

    【讨论】:

    • 抱歉,打错了。唯一的 c 不在实际代码中。
    • 谢谢伙计,这正是问题所在
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-20
    • 2013-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多