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