Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
721 views
in Technique[技术] by (71.8m points)

iphone - custom MKAnnotation class for changing leftCalloutAnnotationView

I have created a custom class for the MKAnnotation

@interface NeighborMapAnnotation : NSObject <MKAnnotation> {
    CLLocationCoordinate2D  coordinate;
    NSString * title;
    NSString * subtitle;
    UIImageView * lcav;
}

@property (nonatomic, readonly) CLLocationCoordinate2D  coordinate;
@property (nonatomic, retain) NSString * title;
@property (nonatomic, retain) NSString * subtitle;
@property (nonatomic, retain) UIImageView * lcav;
@end



NeighborMapAnnotation *neighbor = [[NeighborMapAnnotation alloc] initWithCoordinate:aCoordinate];
//imgView is some UIImageView that I already have
neighbor.leftCalloutAccessoryView = imgView;


- (MKAnnotationView *) mapView:(MKMapView *) mapView viewForAnnotation:(id <MKAnnotation>) annotation
{

    MKPinAnnotationView * annView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"custom view"] autorelease];
    annView.animatesDrop = YES;
    annView.canShowCallout = YES;
    UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    annView.rightCalloutAccessoryView = rightButton;
    annView.leftCalloutAccessoryView = [annotation lcav];//is this right?

    return annView;
}

I want to be able to change the leftCalloutAccessoryView of the annotation to an image. What code should I change in my custom MKAnnotation class?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The leftCalloutAccessoryView property is a property of the MKAnnotationView class (not in the MKAnnotation protocol).

In the viewForAnnotation delegate method of MKMapView, you provide an MKAnnotationView for your annotations. In that method, check if the annotation type is your custom annotation type using isKindOfClass and set the view's properties as needed.

In your NeighborMapAnnotation class, you could store the imageView as a UIImageView property and set that when creating the annotation.

In viewForAnnotation, you would set the view's leftCalloutAccessoryView to the annotation's imageView.

Edit:
Here's one way to implement it...

Add an image property to your custom annotation class:

@interface NeighborMapAnnotation : NSObject <MKAnnotation> {
    ...
    UIImage *image;
}
...  
@property (nonatomic, retain) UIImage *image;
@end

At the place where you create your annotations, set the image property:

NeighborMapAnnotation *neighbor = [[NeighborMapAnnotation alloc] initWithCoordinate:aCoordinate];
neighbor.image = [UIImage imageNamed:@"someimage.png"];
//the image could be different for each annotation
...

In the viewForAnnotation delegate method (make sure map view's delegate is set):

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[NeighborMapAnnotation class]])
    {
        static NSString *AnnotationIdentifier = @"AnnotationIdentifier";
        MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
        if (!pinView)
        {
            pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];
            pinView.canShowCallout = YES;
            pinView.animatesDrop = YES;
        }
        else
        {
            pinView.annotation = annotation;
        }

        UIImageView *leftCalloutView = [[UIImageView alloc] 
            initWithImage:((NeighborMapAnnotation *)annotation).image];
        pinView.leftCalloutAccessoryView = leftCalloutView;
        [leftCalloutView release];

        return pinView;
    }

    return nil;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...