【问题标题】:Get variable and plug it into MKAnnotationView获取变量并将其插入 MKAnnotationView
【发布时间】:2010-12-08 22:10:37
【问题描述】:

我有提供坐标、标题、副标题、图像文件位置信息的代码。此信息用于生成 ios mapkit pin 的注释。标题和副标题会自动插入,我可以在选择 pin 后获取其他信息(图像文件位置)。我希望在注释(pin 弹出窗口)中显示图像的缩略图,但是我无法将其设置为变量以从我的信息中获取它,而不是对其进行硬编码。下面是我用来生成地图/图钉的代码。

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
    // Boilerplate pin annotation code
 UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

 NSString *imageLoc;

    MKPinAnnotationView *pin = (MKPinAnnotationView *) [self.map dequeueReusableAnnotationViewWithIdentifier: @"restMap"];
    if (pin == nil) {
        pin = [[[MKPinAnnotationView alloc] initWithAnnotation: annotation reuseIdentifier: @"restMap"] autorelease];

    } else { 
        pin.annotation = annotation;
 }

    pin.pinColor = MKPinAnnotationColorRed;
    pin.canShowCallout = YES;
    pin.animatesDrop = YES;

 NSString *imageLoc= ????????????

 UIImageView *leftIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageLoc]];
    pin.leftCalloutAccessoryView = leftIconView;

    [detailButton addTarget:self action:@selector(showDetailView:) forControlEvents:UIControlEventTouchUpInside];
    pin.rightCalloutAccessoryView = detailButton;
    return pin;
}

@interface MapPin : NSObject <MKAnnotation> {

    CLLocationCoordinate2D coordinate;
    NSString *subtitle; 
    NSString *title; 
 NSString *indexnumber;
 NSString *imageFile;
}

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, readonly) NSString *title;
@property (nonatomic, readonly) NSString *subtitle;
@property (nonatomic, readonly) NSString *indexnumber;
@property (nonatomic, readonly) NSString *imageFile;

-(id)initWithCoordinates:(CLLocationCoordinate2D)location
               placeName: placeName
             description:description
    indexnum:indexnum
   imageFileLoc:imageFileLoc;

@end


#import "MapPin.h"

@implementation MapPin

@synthesize coordinate;
@synthesize title;
@synthesize subtitle;
@synthesize indexnumber;
@synthesize imageFile;

-(id)initWithCoordinates:(CLLocationCoordinate2D)location
               placeName: placeName
             description:description
    indexnum:indexnum
   imageFileLoc:imageFileLoc{

    self = [super init];
    if (self != nil) {
  imageFile=imageFileLoc;
  [imageFile retain];
  indexnumber=indexnum;
  [indexnumber retain];
        coordinate = location;
        title = placeName;
        [title retain];
        subtitle = description;
        [subtitle retain];
    }
    return self;    
}

【问题讨论】:

  • 请检查您的格式。

标签: iphone ios annotations mapkit mkannotation


【解决方案1】:

注释对象有什么可用的?您是否尝试过以下方法?

NSString *imageLoc = [annotation respondsToSelector:@selector(imageFile)] ? [annotation imageFile] : @"some_default_value";

【讨论】:

    猜你喜欢
    • 2013-04-13
    • 2019-06-20
    • 1970-01-01
    • 2021-06-26
    • 2017-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多