2013-02-13 94 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。