カスタム画像内に背景の画像を表示するカスタム注釈ビューに取り組んでいます。私はこれに基づいてコードを作成しました:カスタム 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;
}
}
そして、私は2つの方法があります:
(void)writeSomething {
}
(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
}
しかし:
- rightButton コールアウトを表示しない (ボタンも表示しない)
- 完全な画像/注釈をクリックすると、 mapView 注釈ビュー メソッドが呼び出されることを好みます。
どうすればできますか?注釈をクリックしても、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];
}
}