0

userLocation과 주석 (plist에서) 사이의 거리를 계산하려고하는데, 매번 mapView를 실행하면 신호 SIGABRT 오류가 발생합니다.신호 SIGABRT, 주석까지 거리를 얻으려고 할 때

내 mapViewController.m에서 calculate를 수행하고 있으며 계산 된 값을 내 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 파일에. 계산

- (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

을하고 내 mapViewController.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

그것은이'setDistance는'그래서 내가 어떻게 MKUserLocation 내부 "setDistance"을 설정하는'MKUserLocation'classm – rptwsthi

+0

@rptwsthi 내에 정의라는 이름의 방법은 없다 말한다 : 여기에 귀하의 코드에 대한 업데이트입니까? – hpetersen

+0

수 없습니다. MKUserLocation에는'distance' 속성이 없습니다. 그것은 한 위치가 아니며 그 위치가 얼마나 정확한 지에 대한 정보입니다. http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKUserLocation_Class/Reference/Reference.html – Craig

답변

0

for-loop에서 주석 거리를 설정하는 경우 self.mapView.annotations 중 하나이므로 주석이 MKUserLocation이 아닌지 확인해야합니다. 그렇지 않으면 "annotation.distance"가 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); 
    } 
}