views:

426

answers:

1

I'm trying to use the "selected" property in MKAnnotationView ( discussed here ), this is for the user to be able to delete a selected annotation...

The following piece of code should find the selected pin in MKMapView and remove it:

CSMapAnnotation *a;

for(a in [mapView annotations])
{
    if([a selected]) //Warning: 'CSMapAnnotation' may not respond to '-selected'
    {
        [mapView removeAnnotation:a];
    }
}

Where CSMapAnnotation is my Custom Map Annotation defined like this:

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

// types of annotations for which we will provide annotation views. 
typedef enum {
    kCMapAnnotationTypeStart        = 0,
    kCMapAnnotationTypeCheckpoint   = 1,
    kCMapAnnotationTypeEnd          = 2
} CSMapAnnotationType;

@interface CSMapAnnotation : NSObject <MKAnnotation>
{
    CLLocationCoordinate2D coordinate;
    CSMapAnnotationType    annotationType;
    NSString*              title;
    NSString*              userData;
}

-(id) initWithCoordinate:(CLLocationCoordinate2D)inCoordinate 
          annotationType:(CSMapAnnotationType) annotationType
                   title:(NSString*)title;

- (BOOL) isEqualToAnnotation:(CSMapAnnotation *) anAnnotation;

@property (nonatomic, readwrite) CLLocationCoordinate2D coordinate;
@property (nonatomic, readwrite) CSMapAnnotationType    annotationType;
@property (nonatomic, retain) NSString* title;
@property (nonatomic, retain) NSString* userData;

I think that because I'm not really "inheriting" from MKAnnotationView, CSMapAnnotation will not respond to selected.

What would be the best way to fix this issue ??

A: 

You are right in your assumption; since CSMapAnnotation does not inherit from MKAnnotationView, and you have not implemented the selected property, it will not work.

Also, are you managing the CSMapAnnotation to MKAnnotationView relationship to map the annotation view (the pin) to the data stored in CSMapAnnotation? Remember that the MKAnnotationView has the selected property, not the MKAnnotation.

If you are managing annotation to view mapping correctly, this should work for you:

CSMapAnnotation *a; 

for(a in [mapView selectedAnnotations])
{
    //You may want a type-check here
    [mapView removeAnnotation:a];
}

Or even:

[mapView removeAnnotations:[mapView selectedAnnotations]];
Chip Coons