2014-10-04 1 views
1

나는 각 위치의 latitudelongitudeimage을 포함하는 Picture의 종류가있다. MapView에 특수 효과로 추가하고 해당 위치의 핀 대신 이미지를 표시하고 싶습니다.MapView의 각 맞춤 주석에 이미지를 추가하는 방법은 무엇입니까?

@interface Annotation : MKAnnotationView <MKAnnotation> 

@property (nonatomic,assign) CLLocationCoordinate2D coordinate; 
@property (nonatomic,copy) NSString *title; 
@property (nonatomic,copy) NSString *subtitle; 
@property (nonatomic,retain) UIImage *image; 


@end 

그리고

@implementation Annotation 

@synthesize title,subtitle,coordinate,image; 


@end 

을 이제 메인 코드에서 나는 이것을 시도하고있다 :

내가하는 사용자 정의 주석이

CLLocationCoordinate2D location; 
Annotation *myAnn; 

for(Pictures *pic in picturesFromDB) 
{ 
myAnn = [[Annotation alloc] init]; 
location.latitude = [pic.langT doubleValue]; 
location.longitude = [pic.longT doubleValue]; 
myAnn.coordinate = location; 
myAnn.title = pic.descript; 


myAnn.image = [UIImage imageWithData:pic.image]; //Not sure about this line! 


[self.mapView addAnnotation:myAnn]; 

}

해야합니까 대의원이 필요하다. "viewForAnnotation"을 호출해야합니까? 작동하지 않아 위임자에게이 작업을 수행했습니다.

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    // If it's the user location, just return nil. 
    if ([annotation isKindOfClass:[MKUserLocation class]]) 
     return nil; 

    // Handle any custom annotations. 
    if ([annotation isKindOfClass:[Annotation class]]) 
    { 
     // Try to dequeue an existing pin view first. 
     MKAnnotationView *pinView = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"]; 
     if (!pinView) 
     { 
      // If an existing pin view was not available, create one. 
      pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomPinAnnotationView"]; 
      //pinView.animatesDrop = YES; 
      pinView.canShowCallout = YES; 

      pinView.image = [UIImage imageNamed:@"image.png"];    
      pinView.calloutOffset = CGPointMake(0, 4); 

     } else { 

      pinView.annotation = annotation; 
     } 
     return pinView; 
    } 
    return nil; 
} 

이 작업은 정상적으로 작동하지만 모든 이미지가 동일한 위치에 추가됩니다. 하지만 각 위치에 위의 image.png 대신 표시 할 특정 이미지가 있습니다.

위치 개수는 가변적이며 동적입니다.

어떻게 이미지를 대리인에게 전달할 수 있습니까? 전달 된 주석에서 이미지 필드를 찾으려고 시도했지만 존재하지 않습니다! 나는 어떤 제안을 주셔서 감사합니다.

답변

2

당신이 속성에 액세스 할 수 있도록 사용자 정의 클래스에 주석을 캐스팅 할 필요 -

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

    // Handle any custom annotations. 
    if ([annotation isKindOfClass:[Annotation class]]) 
    { 
     Annotation *myAnn=(Annotation *)annotation; 
     // Try to dequeue an existing pin view first. 
     MKAnnotationView *pinView = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"]; 
     if (!pinView) 
     { 
      // If an existing pin view was not available, create one. 
      pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomPinAnnotationView"]; 
      //pinView.animatesDrop = YES; 
      pinView.canShowCallout = YES;    
      pinView.calloutOffset = CGPointMake(0, 4); 

     } else { 
      pinView.annotation = annotation; 
     } 
     pinView.image = myAnn.image; 
     return pinView; 
    } 
    return nil; 
} 
관련 문제