Im trying to show a callout when user taps on an overlay. The callout then has a title on it based on the selected overlay. I want the annotations to be shown only when users taps on an overlay. but the problem is that the overlay doesnt recognize the tap and all the annotations are visible at start. I want them hidden.
a similiar question is here. but I cant figure it out. Show callout when tapping overlay
overlays coordinations are downloaded from the server and added like this:
//Add a polygon
MKPolygon *rect=[MKPolygon polygonWithCoordinates:parkingCords count:5];
[self.mapView addOverlay:rect];
[self.mapView addAnnotation:rect];
Each overlay has now an Annotation in its centre.
ViewForAnnotation
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
...
else if([annotation isKindOfClass:[MKPolygon class]]){
NSLog(@"MKPOLYGON CLASS");
static NSString *identifier3 = @"else";
MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier3];
if (annotationView == nil) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier3];
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
} else {
annotationView.annotation = annotation;
}
...
}
viewForOverlay
-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay{
if([overlay isKindOfClass:[MKPolygon class]]){
MKPolygonView *view = [[MKPolygonView alloc] initWithOverlay:overlay];
view.lineWidth=1;
view.strokeColor=[UIColor blueColor];
view.fillColor=[[UIColor blueColor] colorWithAlphaComponent:0.3];
recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(overlayTapped)];
recognizer.delegate=self;
[view addGestureRecognizer:recognizer];
[recognizer release];
return view;
}
return nil;
}
-(void)overlayTapped{
NSLog(@"overlay tapped");
//[self.mapView setSelectedAnnotations:?????];
}