【问题标题】:Getting Longitude and Latitude Values获取经度和纬度值
【发布时间】:2011-10-24 16:38:14
【问题描述】:

我正在尝试从我的代码连接到 GPS。我是按照to this tutorial 这样做的。

- (void)viewDidLoad {
    [super viewDidLoad];

    CLController = [[CoreLocationController alloc] init];
    CLController.delegate = self;
    [CLController.locMgr startUpdatingLocation];
 }

- (void)locationUpdate:(CLLocation *)location {
    locLabel.text = [location description];
  }

- (void)locationError:(NSError *)error {
    locLabel.text = [error description];
  }

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
  }

上面的代码放在一个叫做GetMyLocationViewController的视图控制器中,我还有另一个叫做MainScreenViewController的视图控制器。

当屏幕加载时,MainScreenViewController 被加载,我需要 GPS 位置才能继续使用此屏幕进行操作。

MainScreenViewControllerViewDidLoad方法中我写了以下内容;

- (void)viewDidLoad {
    [super viewDidLoad];

    GetMyLocationViewController *getMyLocationViewController = [[GetMyLocationViewController alloc]initwithXib:nil bundle:nil];

    [self.navigationController pushViewController:getMyLocationViewController Animation:YES];
    // AND THEN I NEED TO ACCESS THE LONGITUDE AND LATITUDE VALUES

 }

当上面的代码被执行时,MainScreenViewControllerviewDidLoad 方法被执行,而不是locationUpdate 方法。我可以获得longitudelatitude 的值的唯一方法是执行locationUpdate 方法。那么如何获取这些值呢?

【问题讨论】:

    标签: iphone objective-c ios cocoa-touch core-location


    【解决方案1】:

    您是否在设备上进行过测试? 4.2版本之前的xcode没有GPS模拟器,因为这个方法locationUpdate从不调用。

    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
        if([self.delegate conformsToProtocol:@protocol(CoreLocationControllerDelegate)]) {  // Check if the class assigning itself as the delegate conforms to our protocol.  If not, the message will go nowhere.  Not good.
            [self.delegate locationUpdate:newLocation];
        }
    }
    

    【讨论】:

    • 那你是说ViewDidLoad之后,locationUpdate被调用了吗?并且在调用ViewDidLoad 后不退出?
    • 您需要在答案中实现复制协议,此方法仅在 GPS 获得新坐标时才会更新。
    • 什么意思?我是新手:S你能详细说明一下吗
    • 复制到实现文件
    【解决方案2】:

    您确定要加载您的GetMyLocationViewController 吗?您的代码仅显示加载MainScreenViewController,它在其-viewDidLoad 方法中再次加载自身,这将导致加载和推送MainScreenViewControllers 的无限循环。

    更新:教程中的 CoreLocationController 类似乎没有必要。与其使用它,不如让 CLLocationManager 成为 GetMyLocationViewController 的属性。使 GetMyLocationViewController 的 -viewDidLoad 方法如下所示:

    -(void)viewDidLoad {
         [super viewDidLoad];
    
         self.locationManager = [[CLLocationManager alloc] init];
         self.locationManager.delegate = self;
         [self.locationManager startUpdatingLocation];
    }
    

    不要忘记导入 CoreLocation 库并实现委托方法。

    【讨论】:

    • 哦,对不起,我编辑了代码。我输入了错误的代码(查找修改后的代码)。你对我应该做什么有任何线索吗?
    猜你喜欢
    • 2019-03-25
    • 2016-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-30
    • 1970-01-01
    相关资源
    最近更新 更多