【问题标题】:My Custom MKAnnotationView isn't clickable我的自定义 MKAnnotationView 不可点击
【发布时间】:2013-07-17 11:06:51
【问题描述】:

我正在开发一个显示背景图像的自定义注释视图,其中包含自定义图像。 我的代码基于此:Custom MKAnnotationView with frame,icon and image 而我的实际代码是:

    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 方法? 在这张地图中,我还有更多不同类型的引脚(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];
    }
}

【问题讨论】:

  • 设置userInteractionEnabled = YES; ?
  • 我试过 annotationImageView.userInteractionEnabled = YES;但不起作用...当我单击时没有任何反应。
  • 这是我的 viewForAnnotation 的完整代码:gist.github.com/anonymous/6224519e308d6bf56c4c
  • 您在 ImagePin 中返回一个空白 title。如果注释的标题为空,则不会显示标注(即使 canShowCallout 为 YES)。
  • @anna-karenina 你说得对!有没有办法不显示标注而只调用操作 calloutAccessoryControlTapped ?我不想在我的自定义图像图钉上显示标注。

标签: iphone ios objective-c mkannotationview


【解决方案1】:

我都修好了! 问题是标题不能为空(我使用 @""... 和 @"AAA 可以正常工作)然后不显示我所做的标注:

annotationView.canShowCallout = NO;

并实施:

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

非常感谢 Anna(解决方案是你的)!

【讨论】:

    【解决方案2】:

    首先结帐您是否将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 ");
    }
    

    更新:

    尝试使用自定义按钮而不是自定义按钮...

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

    【讨论】:

    • 委托很好,因为我有其他类型的引脚(mkpinannotation),并且工作正常并且还调用 calloutAccessoryControlTapped。
    • @RiccardoRossi 我现在更新回答使用-(void)writeSomething:(id)sender 而不是你定义的......
    • -(void)writeSomething:(id)sender 也没有任何反应(并且没有显示 rightButton)。
    • 我会检查你的整个代码并发表评论,但是从这个链接github.com/ryanmaxwell/GoogleMapsCalloutView/blob/master/… 看到另一个演示,比如你的要求 :) 我希望它对你有帮助....
    【解决方案3】:

    对于在实现自定义 MKAnnotationView 时偶然发现这篇文章的任何其他人,问题可能还在于您必须明确设置视图的框架。

    MKMapView MKPointAnnotation tap event not called

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-30
      • 2013-08-19
      • 2011-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多