【问题标题】:iPhone 3.0 Compass: how to get a heading?iPhone 3.0 指南针:如何获取航向?
【发布时间】:2009-06-25 12:53:29
【问题描述】:

我对 Objective-C 比较陌生,对它还不太了解,所以对于这个可能是一个非常业余的问题,我深表歉意。

我正在尝试从 CLHeading 和 CLLocationDirection 获取磁航向。但是我收到了这行代码的编译错误:

locationLabel.text = [[[location course] magneticHeading] stringValue];

错误是:

warning: invalid receiver type 'CLLocationDirection'  
error: cannot convert to a pointer type

我真的不明白我在这里做错了什么。请帮忙!

【问题讨论】:

    标签: iphone cllocation


    【解决方案1】:

    以下是使用指南针所需的步骤。

    1)检查可用性:如果位置管理器的headingAvailable属性为YES,则可以使用指南针。

    2) 使用位置管理器方法-(void) startUpdatingHeading 开始接收您正在搜索的信息

    3) 使用委托方法实际检索此信息(不要忘记将自己设置为委托)

     - (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
    

    希望这会有所帮助。

    【讨论】:

    • 看在 Google 的份上:headingAvailable 属性自 iO4 以来已被弃用。现在有一个返回布尔值的 headingAvailable 类方法。
    【解决方案2】:

    magneticHeading 是 CLLocationDirection 类型,它只是原始数据类型“double”的 typedef。在您的示例中,您试图向不是对象的东西发送消息!您应该像这样简单地格式化双精度:

    locationLabel.text = [NSString stringWithFormat:@"Heading %.3f", [[location course] magneticHeading]];

    【讨论】:

    • 谢谢,这是我真正感到困惑的地方。我想我需要了解有关 typedef 的更多信息。
    【解决方案3】:

    您如何分配和初始化location?确保将location 定义为(CLLocationDirection *) 而不仅仅是(CLLocationDirection)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多