Сигнал SIGABRT, при попытке получить расстояние до аннотаций
Я пытаюсь вычислить расстояние между userLocation и моими аннотациями (из plist), но каждый раз, когда я запускаю mapView, я получаю сигнал SIGABRT error.
Я делаю вычисления в моем mapViewController.m и хочу показать вычисленные значения в моем tableViewController.
В моих аннотациях
#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);
}
}
И в моей таблице ViewController.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 ответ
В цикле 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);
}
}