获取变量并将其插入MKAnnotationView

时间:2010-12-08 22:10:37

标签: iphone ios annotations mapkit mkannotation

我有代码,我给出了坐标,标题,副标题,图像文件位置信息。此信息用于生成ios mapkit引脚的注释。标题和副标题会自动插入,我可以在选择引脚后获取其他信息(图像文件位置)。我希望在注释(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;    
}

1 个答案:

答案 0 :(得分:0)

注释对象有哪些内容?你试过以下这个吗?

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