My custom MKAnnotationView is not clickable

I am working on a custom annotation displaying a background image inside an inline image. I based my code on this: Custom MKAnnotationView with frame, icon and image And my code:

else if ([annotation isKindOfClass:[ImagePin class]]){ static NSString *identifierImage = @"ImagePinLocation"; MKAnnotationView *annotationImageView = (MKAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier:identifierImage]; if(annotationImageView){ return annotationImageView; } else { annotationImageView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifierImage]; annotationImageView.canShowCallout = YES; annotationImageView.image = [UIImage imageNamed:@"image_bg_mappa"]; UIImage *frame = [UIImage imageNamed:@"image_bg_mappa"]; ImagePin *imagePinImage = annotation; NSLog(@"%@", [NSString stringWithFormat:@"%@", imagePinImage.image]); NSString *urlStringForImage = [NSString stringWithFormat:@"%@", imagePinImage.image]; NSURL *urlForImage = [NSURL URLWithString:urlStringForImage]; UIImageView *imageView = [[UIImageView alloc] init]; [imageView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", urlForImage]] placeholderImage:[UIImage imageNamed:@"avatar-placeholder.png"]]; UIGraphicsBeginImageContext(CGSizeMake(annotationImageView.image.size.width, annotationImageView.image.size.height)); [frame drawInRect:CGRectMake(0, 0, frame.size.width, frame.size.height)]; [imageView.image drawInRect:CGRectMake(2, 2, 48, 38)]; // the frame your inner image annotationImageView.image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; [rightButton addTarget:self action:@selector(writeSomething:) forControlEvents:UIControlEventTouchUpInside]; [rightButton setTitle:@"" forState:UIControlStateNormal]; annotationImageView.canShowCallout = YES; annotationImageView.rightCalloutAccessoryView = rightButton; annotationImageView.enabled = YES; return annotationImageView; } } 

And I have two methods:

 (void)writeSomething { } (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control { } 

But:

  • Do not show my rightButton callout (do not show also button)
  • I prefer that when you click on the full image / annotation, it will call the mapView annotationView method.

How can i do this? Why, when I click on the annotation, it does not call my calloutAccessoryControlTapped method? On this map, I also have a different type of pin (mkpinannotation) and it works fine (also the calloutAccessoryControlTapped method works fine with these contacts). What is the problem? Thanks to everyone, and sorry for my poor English (I'm learning it).

EDIT: My full code for the viewForAnnotation method: https://gist.github.com/anonymous/6224519e308d6bf56c4c MKPinAnnotation works fine.

EDIT: This is my ImagePin.h

 #import <Foundation/Foundation.h> #import <MapKit/MapKit.h> @interface ImagePin : NSObject <MKAnnotation> - (id)initWithImage:(NSString*)image imagebeachId:(NSString*)imagebeachId coordinate:(CLLocationCoordinate2D)coordinate; //- (MKMapItem*)mapItem; @property (nonatomic, copy) NSString *imagebeachId; @property (nonatomic, copy) NSString *image; @property (nonatomic, assign) CLLocationCoordinate2D theCoordinate; @end 

And my ImagePin.m

 #import "ImagePin.h" #import <AddressBook/AddressBook.h> @interface ImagePin () @end @implementation ImagePin @synthesize image = _image; @synthesize imagebeachId = _imagebeachId; @synthesize theCoordinate = _theCoordinate; - (id)initWithImage:(NSString*)image imagebeachId:(NSString*)imagebeachId coordinate:(CLLocationCoordinate2D)coordinate { if ((self = [super init])) { //self.address = address; self.image = image; self.imagebeachId = imagebeachId; self.theCoordinate = coordinate; } return self; } - (NSString *)title { return @""; } - (NSString *)subtitle { return _image; } - (CLLocationCoordinate2D)coordinate { return _theCoordinate; } @end 

EDIT: Here I add the annotation:

 - (void)imagePositions:(NSDictionary *)responseData { for (id<MKAnnotation> annotation in self.mapView.annotations) { if ([annotation isKindOfClass:[ImagePin class]]){ //[self.mapView removeAnnotation:annotation]; } } for (NSArray *row in responseData) { NSString * imageBeachId = [row valueForKey:@"id"]; NSNumber * latitude = [row valueForKey:@"lat"]; NSNumber * longitude = [row valueForKey:@"lng"]; NSString * imageBeach = [row valueForKey:@"fb_photo_url"]; CLLocationCoordinate2D coordinate; coordinate.latitude = latitude.doubleValue; coordinate.longitude = longitude.doubleValue; ImagePin *annotation = [[ImagePin alloc] initWithImage:imageBeach imagebeachId:imageBeachId coordinate:coordinate]; [self.mapView addAnnotation:annotation]; } } 
+4
source share
3 answers

I fixed everything! The problem is that the title cannot be empty (I used @ "... with @" AAA works fine) and then in order not to show the callouts I made:

 annotationView.canShowCallout = NO; 

and implemented:

 -(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view { //here you action } 

BIG thanks to Anna (your decision)!

+4
source

First, verify that you are delegating the MapView or not; if not, give the delegate as shown below.

 yourMapView.delegate = self; 

Also in the .h file, specify delegate , as shown below.

 @interface ViewController : UIViewController<MKMapViewDelegate,MKAnnotation>{... 

after checking that this method below is called when the accessory is knocked ...

 - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control { NSLog(@"Do something "); } 

UPDATE:

Instead of a custom button, try using it ...

 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { NSLog(@"viewForAnnotation..."); static NSString *identifier = @"MyLocation"; if ([annotation isKindOfClass:[ImagePin class]]) { MKAnnotationView *annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier]; if (annotationView == nil) { annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier] autorelease]; } if (annotation == mapView.userLocation) return nil; annotationView.image = [UIImage imageNamed:@"yourImageName.png"]; annotationView.annotation = annotation; annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; annotationView.rightCalloutAccessoryView.tag = 101; annotationView.canShowCallout = YES; // annotationView.animatesDrop = YES; return annotationView; } return nil; } 
+1
source

For anyone else who stumbles upon this post while implementing a custom MKAnnotationView, the problem could also be that you must explicitly set the frame of your view.

Inactive event MKMapView MKPointAnnotation is not raised

0
source

Source: https://habr.com/ru/post/1491852/


All Articles