从多个注释中获取距离

Posted

技术标签:

【中文标题】从多个注释中获取距离【英文标题】:Get distance from multiple annotations 【发布时间】:2013-02-13 15:56:14 【问题描述】:

我已经尝试了几天,并在网上搜索试图解决我的问题,但我找不到任何相关信息。

我正在尝试使用 CustomCell 计算从 userLocation 到我在 UITableView 中的注释的距离。

我能够在 NSLOG 中获得计算出的距离,但在我的 tableView 中,我的所有单元格都获得了 0.0 公里。

谁能看到我在这里做错了什么?

    - (void)viewDidLoad

    [super viewDidLoad];

    self.mapView.delegate = self;
    [self startLocationServices];

    NSString *path = [[NSBundle mainBundle] pathForResource:@"points" ofType:@"plist"];
    NSArray *anns = [NSArray arrayWithContentsOfFile:path];

    for(NSMutableDictionary *note in anns) 
        double doubleLatitude = [[note objectForKey:@"sculptureLatitudeKey"] doubleValue];
        double doubleLongitude = [[note objectForKey:@"sculptureLongitudeKey"] doubleValue];
        Annotation* myAnnotation = [[Annotation alloc] init];
        CLLocationCoordinate2D theCoordinate;
        theCoordinate.latitude = doubleLatitude;
        theCoordinate.longitude = doubleLongitude;
        myAnnotation.coordinate = theCoordinate;
        myAnnotation.title = [note objectForKey:@"sculptureNameKey"];
        myAnnotation.subtitle = [note objectForKey:@"sculptureAddressKey"];
        myAnnotation.sculptureIdKey = [note objectForKey:@"sculptureIdKey"];
        [self.mapView addAnnotation:myAnnotation];
    


- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation

    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 5000, 5000);
    [self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];


- (void)startLocationServices

    if (self.locationManager == nil)
    
        self.locationManager = [CLLocationManager new];
    
    [self.locationManager setDelegate:self];
    [self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    [self.locationManager setDistanceFilter:kCLDistanceFilterNone];

    [self.locationManager startUpdatingLocation];


- (void)stopLocationServices

    [self.locationManager stopUpdatingLocation];
    [self.locationManager setDelegate:nil];



- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
    
    for (Annotation *annotation in self.mapView.annotations)
    
        CLLocationCoordinate2D coord = [annotation coordinate];
        CLLocation *annLocation = [[CLLocation alloc] initWithLatitude:coord.latitude longitude:coord.longitude];

        CLLocationDistance calculatedDistance = [annLocation distanceFromLocation:newLocation];
        NSLog(@"Nice distance:%.1f m\n", calculatedDistance);
    

我的 NSLOG 显示了这一点,而我的 .plist 中只有 3 个注释

2013-02-13 16:46:14.736 TalkingSculpture[91833:c07] Nice distance:762.1 m
2013-02-13 16:46:14.736 TalkingSculpture[91833:c07] Nice distance:98.8 m
2013-02-13 16:46:14.736 TalkingSculpture[91833:c07] Nice distance:2704.3 m
2013-02-13 16:46:14.737 TalkingSculpture[91833:c07] Nice distance:0.0 m

我的 customCell.h

#import <UIKit/UIKit.h>

@interface customCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UILabel *customTitle;
@property (weak, nonatomic) IBOutlet UILabel *customSubTitle;
@property (weak, nonatomic) IBOutlet UILabel *distanceLabel;

@end

还有customCell.m

#import "customCell.h"

@implementation customCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) 
        // Initialization code
    
    return self;


- (void)setSelected:(BOOL)selected animated:(BOOL)animated

    [super setSelected:selected animated:animated];

    // Configure the view for the selected state


@end

在表格视图中我填充了 cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    
    static NSString *CellIdentifier = @"Cell";
    customCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    if (!cell)
    
        cell = [[customCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    
    cell.customTitle.text = [[self.locationsArray objectAtIndex:indexPath.row] valueForKey:@"sculptureNameKey"];
    cell.customSubTitle.text = [[self.locationsArray objectAtIndex:indexPath.row] valueForKey:@"sculptureAddressKey"];
    cell.distanceLabel.text = [NSString stringWithFormat:@"%.1f km\n", _calulatedDistance];

    return cell;

【问题讨论】:

你能显示在自定义单元格中设置距离的代码吗? 单元格中的文本格式是否正确?例如 %.1f? @AnnaKarenina 我刚刚用来自 customCell 和 cellForRowAtIndexPath 的更多代码更新了我的问题 _calulatedDistance 是如何设置的?它的值不应该基于当前indexPath处的对象吗? @AnnaKarenina 我不明白你的意思? 【参考方案1】:

不知道你是否还需要这个问题的答案。但是每次显示表格视图时,您都需要填充一个数组(或替代)来保存所有当前距离。然后在 cellForRowAtIndexPath 中将标签设置为与该数组的适当距离。您在此代码中所做的是将它设置为 ivar,当您调用它时,它是 nil 或 0。

【讨论】:

以上是关于从多个注释中获取距离的主要内容,如果未能解决你的问题,请参考以下文章

加油站会员管理小程序实战开发教程07-如何计算距离

在 solr 3.6 中获取与 geohash 字段的距离

计算我的位置与多个位置之间的距离

AngularJS获取多个坐标之间的距离

计算路线长度

如何使用 PHP 从距离矩阵中获取集群?