信号SIGABRT,当试图获得注释的距离

时间:2013-02-15 10:16:49

标签: objective-c cocoa-touch mkmapview userlocation cllocationdistance

我正在尝试计算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

1 个答案:

答案 0 :(得分:0)

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

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);
    }
}