0

I am trying to set custom annotation title value but I got the crash report.crash report is:

"Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CustomAnnotation setTitle:]: unrecognized selector sent to instance"

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
    MKMapRect mapRect = mapView.visibleMapRect;
    MKMapPoint eastMapPoint = MKMapPointMake(MKMapRectGetMinX(mapRect), MKMapRectGetMidY(mapRect));
    MKMapPoint westMapPoint = MKMapPointMake(MKMapRectGetMaxX(mapRect), MKMapRectGetMidY(mapRect));

    CGFloat meters = MKMetersBetweenMapPoints(eastMapPoint, westMapPoint);

    if (meters > 15000)
        return;

    // if we have some backlogged requests, let's just get rid of them

    [self.searchQueue cancelAllOperations];

    // issue new MKLoadSearch

    [self.searchQueue addOperationWithBlock:^{

        __block BOOL done = NO;

        MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
        request.naturalLanguageQuery = @"theater";
        request.region = mapView.region;

        MKLocalSearch *localSearch = [[MKLocalSearch alloc] initWithRequest:request];
        [localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {

            NSMutableArray *annotations = [NSMutableArray array];



            [response.mapItems enumerateObjectsUsingBlock:^(MKMapItem *item, NSUInteger idx, BOOL *stop) {

                for (CustomAnnotation *annotation in mapView.annotations)
                {
                    // if we don't need this one, don't add it, just return, and we'll check the next one

                    annotation.title;

                    NSLog(@"%@",annotation.title);

                    if ([annotation isKindOfClass:[CustomAnnotation class]])
                        if (item.placemark.coordinate.latitude == annotation.coordinate.latitude &&
                            item.placemark.coordinate.longitude == annotation.coordinate.longitude)
                        {
                            return;
                        }
                }


                NSLog(@"%f",item.placemark.coordinate.latitude);

                NSLog(@"%f",item.placemark.coordinate.longitude);
                // otherwise add it


                CustomAnnotation *annotation = [[CustomAnnotation alloc] initWithPlacemark:item.placemark];

                CLLocation *locA = [[CLLocation alloc] initWithLatitude:37.33554 longitude:-121.885209];

                CLLocation *locB = [[CLLocation alloc] initWithLatitude:item.placemark.coordinate.latitude longitude:item.placemark.coordinate.longitude];

                CLLocationDistance distance = [locA distanceFromLocation:locB];
                NSLog(@"%f",distance);

                annotation.title=item.title;     --> i get crash report in this line
                NSLog(@"%@",annotation.title);
                annotation.phone = item.phoneNumber;
                annotation.subtitlee = item.placemark.addressDictionary[(NSString *)kABPersonAddressStreetKey];
                [annotations addObject:annotation];

                NSLog(@"%@",annotations);

                //[self.mapView addAnnotations:annotation.name];
            }];




            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                //CustomAnnotation

                [self.mapView addAnnotations:annotations];
            }];

            done = YES;
        }];

        while (!done)
            [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
                                     beforeDate:[NSDate distantFuture]];
    }];


}

///////////////////////////////////

thank you...

enter code here



#import <MapKit/MapKit.h>

@interface CustomAnnotation : MKPlacemark
{
    CLLocationCoordinate2D coordinate;
}

@property (strong, nonatomic) NSString *title;
@property (strong, nonatomic) NSString *subtitlee;
@property (strong, nonatomic) NSString *phone;
@property (nonatomic) NSUInteger index;
enter code here

@end

Barani
  • 84
  • 1
  • 1
  • 12

2 Answers2

0

Is your CustomAnnotation class inherit from MKAnnotation ? MKAnnotation defined

@property(nonatomic, readonly, copy) NSString *title
kai
  • 310
  • 3
  • 14
0

I figured it out. CustomAnnotation is a subclass of MKPlacemark, which conforms to the MKAnnotation protocol. The MKAnnotation protocol has got a property title.

So you are actually inheriting this property from the MKAnnotation protocol, but this is not what you want. Just rename your title property to e.g. customAnnotationTitle and you should be fine.

oyvindhauge
  • 3,496
  • 2
  • 29
  • 45