开发者

Get variable and plug it into MKAnnotationView

I have code which i gives coordinates, title, subtitle, image file location information. This information is used to generate the annotation for a ios mapkit pin. The title and subtitle are automatically plugged in and i can grab the other info (image file location) after the pin is selected. I am looking to display a thumbnail of the image in the annotation (pin popup) however but i am having trouble setting it as a variable to get it from my info as opposed to hard coding it. Below is the code i use to generate my map/ pins.

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


What's available from the annotation object? Have you tried the following?

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜