views:

175

answers:

1

I have a map marker to which I've added an event listener. when I click on the marker, I can get it to NSLog out a message... yet when I then click on the map, it does the same. I don't know if this is usual behaviour? In the end, I am trying to get a popup viewcontroller appearing - but that's been put on hold until this works.

So... I have sub-classed the annotationview which looks like this

- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {
    if(self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]) {
        self.image  = [UIImage imageNamed:@"numberplate.png"];
        self.frame = CGRectMake(0,0,133,40);
        self.centerOffset = CGPointMake(76,0);
        //self.selected = NO;
        self.canShowCallout = NO;
    }
    return self;
}

Then in my main view controller, I have

- (MKAnnotationView *)mapView:(MKMapView *)lmapView viewForAnnotation:(id <MKAnnotation>)annotation {

    VehicleViewInfo *eventView = (VehicleViewInfo *)[lmapView
                                                     dequeueReusableAnnotationViewWithIdentifier:
                                                     @"eventview"];
    if(eventView == nil) {
        eventView = [[[VehicleViewInfo alloc] initWithAnnotation:annotation
                                                 reuseIdentifier:@"eventview"]
                     autorelease];
    }

    [eventView addObserver:self
                forKeyPath:@"selected"
                   options:NSKeyValueObservingOptionNew
                   context:GMAP_ANNOTATION_SELECTED];

    eventView.annotation = annotation;
    return eventView;
}

So then in my event listener, I have

- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context{

    NSString *action = (NSString*)context;
    NSLog(@"action received: %@", context);

    if([action isEqualToString:GMAP_ANNOTATION_SELECTED]){
        NSLog(@"ooh you clicked the annotation!");
    }
}

I also set the GMAP_ANNOTATION_SELECTED as

NSString * const GMAP_MAP_SELECTED = @"mapselected";

The output I get when I click the marker is as expected. But when I click the map area, I get the same response! (as if I'd clicked on the map).

Do I need to add some sort of listener to the mkmap to cancel all annotations or something??

A: 

You'll get a notification each time the property "selected" is changed. So you will get this notification when your view is deselected. So, if you have just selected this annotation and then tapped somewhere else to deselect it, you will have received this notification twice.

BTW, subtle possible bug in your code: if you end up dequeueing this annotation view, you may be registering the observer more than once. You need to make sure you remove the observer when the view is enqueued OR simply register the observer once when the view is created (within the 'if (eventView == nil)'

Firoze Lafeer
ah yes - that makes sense! OK, I've added a BOOL check in there now which is working nicely to say if it's selected or deselected. Many thanks.
Matt Facer