2013-02-13 64 views
1

我一直在尝试几天,并搜索网络来尝试解决我的问题,但我找不到任何相关信息。获取多个注释的距离

我试图计算从userLocation到UITableView中的注释与CustomCell之间的距离。

我能够在NSLOG中获得计算距离,但在我的tableView即时获得0.0km在我所有的细胞。

任何人都可以看到我在这里做错了吗?

- (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 

在表格视图中填充cellFor RowAtIndexPath

- (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; 
} 
+0

你可以显示在自定义单元格中设置距离的代码吗? – Anna 2013-02-13 16:56:14

+0

您是否正在格式化单元格中的文字? %.1f例如? – Ondrej 2013-02-13 16:56:38

+0

@AnnaKarenina我刚刚用customCell的更多代码更新了我的问题,并且cellForRowAtIndexPath – hpetersen 2013-02-13 17:24:16

回答

0

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