我正在开发一个自定义注释视图,它在自定义图像内部显示背景图像。 我的代码基于此: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 {
}
可是:
我怎么做?为什么当我点击注释时,它不会调用我的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 :(得分:5)
我已经修好了! 问题是标题不能为空(我使用@“”......带有@“AAA工作正常)然后不显示我已完成的标注:
annotationView.canShowCallout = NO;
并实施:
-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
//here you action
}
非常感谢安娜(解决方案就是你)!
答案 1 :(得分: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 ");
}
<强>更新强>
而不是自定义按钮尝试使用它...
- (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;
}
答案 2 :(得分:0)
对于在实现自定义MKAnnotationView时偶然发现此帖子的其他人,问题可能还在于您必须明确设置视图的框架。