Note that there are some explanatory texts on larger screens.

plurals
  1. POMy Custom MKAnnotationView isn't clickable
    text
    copied!<p>i'm working on a custom annotationview that show a image of background, with inside a custom image. I've based my code from this: <a href="https://stackoverflow.com/questions/10520274/custom-mkannotationview-with-frame-icon-and-image">Custom MKAnnotationView with frame,icon and image</a> And my actually code is:</p> <pre><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; } } </code></pre> <p>And then i have two method:</p> <pre><code>(void)writeSomething { } (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control { } </code></pre> <p>But:</p> <ol> <li>Not show my rightButton callout(not show also the button)</li> <li>I prefer that when i click on the full image/annotation it call mapView annotationView method.</li> </ol> <p>How i can do it ? Why when i click on annotation, it not call my calloutAccessoryControlTapped method ? In this map i have also more different type of pin(mkpinannotation), and works fine(also the method calloutAccessoryControlTapped works fine with these pins). Where is the problem ? Thanks to all, and sorry for my bad english(I'm learning it).</p> <p><strong>EDIT:</strong> My full code for viewForAnnotation method: <a href="https://gist.github.com/anonymous/6224519e308d6bf56c4c" rel="nofollow noreferrer">https://gist.github.com/anonymous/6224519e308d6bf56c4c</a> The MKPinAnnotation works fine.</p> <p><strong>EDIT:</strong> This is my ImagePin.h</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; #import &lt;MapKit/MapKit.h&gt; @interface ImagePin : NSObject &lt;MKAnnotation&gt; - (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 </code></pre> <p>And my ImagePin.m</p> <pre><code>#import "ImagePin.h" #import &lt;AddressBook/AddressBook.h&gt; @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 </code></pre> <p><strong>EDIT:</strong> This is where i add annotation:</p> <pre><code>- (void)imagePositions:(NSDictionary *)responseData { for (id&lt;MKAnnotation&gt; 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]; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload