2013-07-17 65 views
4

我正在处理自定义注释视图,显示背景的图像,自定义图像内。 我根据我的代码从这个:Custom MKAnnotationView with frame,icon and image 而我居然代码:我的自定义MKAnnotationView不可点击

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; 
     } 
    } 

然后我有两个方法:

(void)writeSomething {   
} 

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

但是:

  1. 不显示我的rightButton标注(不显示按钮)
  2. 我更喜欢当我点击完整图像/注释它调用mapView annotationView方法。

我该怎么办呢?为什么当我点击注释时,它不会调用我的calloutAccessoryControlTapped方法? 在这张地图中,我也有更多不同类型的pin(mkpinannotation),并且正常工作(calloutAccessoryControlTapped方法也可以很好地与这些引脚一起工作)。 问题在哪里? 感谢所有人,并为我的坏英语感到抱歉(我正在学习它)。

编辑:我对viewForAnnotation方法全码:https://gist.github.com/anonymous/6224519e308d6bf56c4c 的MKPinAnnotation工作正常。

编辑: 这是我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 

而且我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 

编辑: 这是我添加注释:

- (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]; 
    } 
} 
+0

set'userInteractionEnabled = YES;'? –

+0

我试着用annotationImageView.userInteractionEnabled = YES;但没有作品...当我点击没有任何反应。 –

+0

这是我完整的viewForAnnotation代码:https://gist.github.com/anonymous/6224519e308d6bf56c4c –

回答

1

,你给delegate到的MapView与否,如果没有则给delegate像波纹管一结账..

yourMapView.delegate = self; 
.h文件

而且定义delegate像波纹管..

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

后检查下面的方法称为配件开关...

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

UPDATE:

取而代之的自定义按钮尝试使用它...

- (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; 
} 
+0

委托没问题,因为我有其他类型的pin(mkpinannotation),并且工作正常,并且还调用calloutAccessoryControlTapped。 –

+0

@RiccardoRossi我现在更新回答使用' - (void)writeSomething:(id)sender'而不是你定义的.. –

+0

这是我的完整代码:https://gist.github.com/anonymous/6224519e308d6bf56c4c –

4

我有固定的一切! 问题是,标题不能为空(我正在使用@“”...用@“AAA正常工作),然后为不显示标注我做:

annotationView.canShowCallout = NO; 

和实施:

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

非常感谢安娜(解决办法是你的)