【问题标题】:Display the distance between a user's current location and an annotation in a Label在标签中显示用户当前位置与注释之间的距离
【发布时间】:2013-01-28 07:11:01
【问题描述】:

我想在自定义表格单元格的标签中显示用户当前位置与另一个注释(显示在我的 MapView 上)之间的距离。

每个单元格显示咖啡馆的名称(nameLabel),在下面,我希望它显示与每个咖啡馆的距离(distanceLabel)。

我的 MapViewController.m 使用此代码计算用户当前位置与最近的咖啡馆之间的距离:

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

    for (MapViewAnnotation *annotation in self.mapView.annotations) {
        CLLocationCoordinate2D coord = [annotation coordinate];
        CLLocation *userLocation = [[CLLocation alloc] initWithLatitude:coord.latitude longitude:coord.longitude];
        annotation.distance = [newLocation distanceFromLocation:userLocation];
        CLLocationDistance calculatedDistance = [userLocation distanceFromLocation:userLocation];
        annotation.distance = calculatedDistance;
    }

我已经设置了我的自定义单元格,我只想知道我应该在我的 TableView 代码 (TableViewController.m) 中添加什么,以便在我的文本标签(称为 distanceLabel)。

例如我该如何完成这条线?

cell.distanceLabel.text = 

更新

刚刚尝试添加对我的 TableViewController.h 的引用,但 xcode 不断向我抛出错误“将 'CLLocationDistance' 重新定义为不同类型的符号”。

@class CLLocationDistance; 

@property (nonatomic, strong) CLLocationDistance *calculatedDistance; 

更新

.h 文件

****更新**

**.m 文件****

【问题讨论】:

    标签: objective-c android-mapview tableview cllocation


    【解决方案1】:

    在 fx 中为 CLLocationDistance 计算距离做一个前向引用。你的 .h 。 CLLocationDistance 是作为双精度类型的类型定义,是一种原始数据类型,因此不要在前向引用中使用指针(CLLocationDistance calulatedDistance; 或@property CLLocationDistance calulatedDistance;)。然后执行以下操作:

     cell.distanceLabel.text = [NSString stringWithFormat:@"%f", calculatedDistance]; 
    

    所以事实上你不必创建另一个/新的双精度并将其分配给计算距离。很抱歉有任何困惑..:)

    【讨论】:

    • 见我上面的“更新”。提前感谢您的所有帮助!
    • 你不必对CLLocationDistance的类做前向引用,只作为ivar属性。首先,您将 CLLocationDistance 定义为一个类,然后定义为一个属性,因此错误,您需要重新定义它。只做一个属性。希望它有所帮助;)
    • 好的,就是这样!但现在它给了我这个错误:“不兼容的指针类型从'double *'分配给'NSString *'这行:cell.distanceLabel.text = computedDistance;
    • 哦,我的错:更新了我上面的答案。 :)
    • 我放了双距离=计算距离;进入我的.m控制器,它说“使用未声明的标识符计算距离”,即使它在我的.h文件(@property CLLocationDistance *calculatedDistance)中声明,并在.m文件中合成......我会发疯/写作错了吗?哈哈哈
    猜你喜欢
    • 1970-01-01
    • 2017-04-24
    • 2017-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多