2014-10-04 4 views
1

У меня есть класс Picture, который содержит latitude и longitude и image каждого местоположения. Я хочу добавить их как аннотацию к 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]; 

}

Do I по-прежнему нужен делегат и должен вызвать «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; 
} 
Смежные вопросы