Custom MKAnnotation class to modify leftCalloutAnnotationView

I created a special class for 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 change the leftCalloutAccessoryView annotations to an image. What code should be changed in my MKAnnotation class?

0
source share
2 answers

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

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

NeighborMapAnnotation imageView UIImageView .

viewForAnnotation view leftCalloutAccessoryView imageView.

Edit:
...

:

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

, , :

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

viewForAnnotation (, ):

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

! . viewForAnnotation. , , , . ! !: -)

+1

All Articles