0

我正在尝试计算 userLocation 和我的注释(来自 plist)之间的距离,但是每次我运行 mapView 时都会收到信号 SIGABRT 错误。

我正在我的 mapViewController.m 中进行计算,并希望在我的 tableViewController 中显示计算值。

在我的 Annotations.h

#import <Foundation/Foundation.h>
#import <MapKit/MKAnnotation.h>

@interface Annotation : NSObject <MKAnnotation>

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, assign) CLLocationDistance distance;
@property (nonatomic, copy) NSString * title;
@property (nonatomic, copy) NSString * subtitle;
@property (nonatomic, copy) NSString * sculptureIdKey;

@end

我正在合成距离 = _distance; 在我的 Annotations.m 文件中。

在我的 mapViewController.m 中,我正在计算

- (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];
        annotation.distance = [annLocation distanceFromLocation:newLocation];
        CLLocationDistance calculatedDistance = [annLocation distanceFromLocation:newLocation];
        annotation.distance = calculatedDistance;

        NSLog(@"Calculated Distance:%.2f m\n", calculatedDistance);
    }
}

在我的 tableViewController.h

#import "mainViewController.h"
#import "mapViewController.h"
#import "Annotation.h"

@interface ListViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate, CLLocationManagerDelegate>

@property (readonly) CLLocationCoordinate2D selectedCoordinate;
@property (nonatomic, assign) CLLocationDistance distance;
@property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic, strong) CLLocation *userLocation;
@property (nonatomic, strong) NSArray *locationsArray;


@end

和 tableViewController.m

#import "ListViewController.h"
#import "customCell.h"

@interface ListViewController ()

@end

@implementation ListViewController
@synthesize locationsArray = _locationsArray;
@synthesize locationManager = _locationManager;
@synthesize selectedCoordinate = _selectedCoordinate;
@synthesize distance = _distance;

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.locationsArray count];
}

- (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:@"Nice distance:%f m\n", _distance];

    return cell;
}

我在日志中的输出如下

2013-02-15 11:05:50.769 TalkingSculpture[11639:c07] Calculated Distance:83971.36 m
2013-02-15 11:05:50.770 TalkingSculpture[11639:c07] Calculated Distance:83406.16 m
2013-02-15 11:05:50.771 TalkingSculpture[11639:c07] Calculated Distance:86002.30 m
2013-02-15 11:05:50.771 TalkingSculpture[11639:c07] -[MKUserLocation setDistance:]: unrecognized selector sent to instance 0xee4f080
2013-02-15 11:05:50.772 TalkingSculpture[11639:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MKUserLocation setDistance:]: unrecognized selector sent to instance 0xee4f080'
*** First throw call stack:
(0x1b44012 0x1460e7e 0x1bcf4bd 0x1b33bbc 0x1b3394e 0x3ef8 0x3637e 0x3593f 0x339c5 0x2f175 0x1b03920 0x1ac6d31 0x1aeac51 0x1ae9f44 0x1ae9e1b 0x1a9e7e3 0x1a9e668 0x3a4ffc 0x43ed 0x2535)
libc++abi.dylib: terminate called throwing an exception
4

1 回答 1

0

在设置注释距离的 for 循环中,您需要检查注释是否不是 MKUserLocation,因为它是 self.mapView.annotations 之一。否则,“annotation.distance”将应用于没有距离属性的 MKUserLocation。这是您的代码的更新:

for (Annotation *annotation in self.mapView.annotations)
{
    if (![annotation isKindOfClass:[MKUserLocation class]]) //<-- Need this check
    {
        CLLocationCoordinate2D coord = [annotation coordinate];
        CLLocation *annLocation = [[CLLocation alloc] initWithLatitude:coord.latitude longitude:coord.longitude];
        annotation.distance = [annLocation distanceFromLocation:newLocation];
        CLLocationDistance calculatedDistance = [annLocation distanceFromLocation:newLocation];
        annotation.distance = calculatedDistance;

        NSLog(@"Calculated Distance:%.2f m\n", calculatedDistance);
    }
}
于 2014-12-03T00:44:37.960 回答