2011-01-30 56 views
0

我創建了一個自定義類的MKAnnotation定製MKAnnotation類改變leftCalloutAnnotationView

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

我希望能夠改變註釋的leftCalloutAccessoryView到的圖像。我應該在自定義MKAnnotation類中更改哪些代碼?

+0

根據以下建議編輯上面的代碼 – aherlambang 2011-01-30 19:19:07

回答

1

leftCalloutAccessoryView屬性是MKAnnotationView類的屬性(不在MKAnnotation協議中)。

在MKMapView的viewForAnnotation委託方法中,您爲註釋提供了MKAnnotationView。在該方法中,使用isKindOfClass檢查註釋類型是否是您的自定義註釋類型,並根據需要設置視圖的屬性。

在您的NeighborMapAnnotation類中,您可以將imageView作爲UIImageView屬性存儲並在創建註釋時進行設置。

在viewForAnnotation中,您將視圖的leftCalloutAccessoryView設置爲註釋的imageView。

編輯:
下面就來實現它的一種方式......

圖像屬性添加到您的自定義註解類:

@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; 
} 
+0

這將允許我有不同的leftCalloutAccessoryView不同的圖像針對不同的引腳? – aherlambang 2011-01-30 19:08:23

1

多數民衆贊成在偉大的!我試圖通過幾個屬性擴展我的自定義註釋類。但我沒有訪問viewForAnnotation委託。我錯過了,並在這裏學到的是,我必須在我的自定義註記類上投下額外的屬性。現在它工作!謝謝! :-)