0

I'm trying to calulate the distance between the userLocation and my annotations (from plist), But every time im run the mapView im getting signal SIGABRT error.

I'm doing the calculate in my mapViewController.m and want to show the calculated values in my tableViewController.

In my 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

I'm synthesize distance = _distance; in my Annotations.m file.

In my mapViewController.m im doing the calculations

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

And in my 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

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

My output in log is as follow

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
hpetersen
  • 11
  • 3
  • 1
    It says the there's no method named `setDistance ` defined inside `MKUserLocation ` classm – rptwsthi Feb 15 '13 at 12:24
  • @rptwsthi so how do i set "setDistance" inside MKUserLocation? – hpetersen Feb 15 '13 at 13:13
  • You can't. An MKUserLocation doesn't have a `distance` property. It's a location (and info about how accurate that location is), not a line. http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKUserLocation_Class/Reference/Reference.html – Craig Feb 16 '13 at 05:55
  • @Craig Okay, thank you.. But what i'm doing wrong to get the calculatedDistance in my tableView? Can you see what I need to do? – hpetersen Feb 16 '13 at 10:55
  • Your ListViewController has single distance property. And as far as I can see you never actually set it. – Craig Feb 16 '13 at 19:17
  • By the way, is this for a course somewhere? I ask because someone else who was quite new to all this was on here very recently trying to achieve the same thing. – Craig Feb 16 '13 at 19:19

1 Answers1

0

In your for-loop where you set the annotation distance, you need to check that the annotation is not MKUserLocation since it is one of self.mapView.annotations. Otherwise, "annotation.distance" will apply to MKUserLocation, which doesn't have a distance property. Here's the update to your code:

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);
    }
}
Cody
  • 650
  • 9
  • 16