2010-04-04 6 views
2

J'affiche des images personnalisées sur une carte (au lieu des broches par défaut) en utilisant le code ci-dessous. Cependant, lorsque je tape sur un élément (et que la légende apparaît), l'image revient à la broche rouge par défaut. Comment puis-je conserver mon image personnalisée, même lorsque la légende est affichée?Annotation personnaliséeAfficher les images sur les broches lorsque l'utilisateur clique sur

- (MKAnnotationView *) mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    MKPinAnnotationView *pinAnnotation = nil; 

    if (annotation != mapView.userLocation) 
    { 
     static NSString *pinID = @"mapPin"; 
     pinAnnotation = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:pinID]; 
     if (pinAnnotation == nil) 
      pinAnnotation = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pinID] autorelease]; 

     // Set the image 
     pinAnnotation.image = [UIImage imageNamed:@"TestIcon.png"]; 

     // Set ability to show callout 
     pinAnnotation.canShowCallout = YES; 

     // Set up the disclosure button on the right side 
     UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
     pinAnnotation.rightCalloutAccessoryView = infoButton; 

     [pinID release]; 
    } 

    return pinAnnotation; 
    [pinAnnotation release]; 
} 

Répondre

5

J'ai trouvé en faisant le pinAnnotation un MKAnnotationView plutôt que d'un MKPinAnnotationView, je suis le résultat souhaité. L'image ne se transforme plus en épingle

static NSString *pinID = @"mapPin"; 
    pinAnnotation = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:pinID]; 
    if (pinAnnotation == nil) 
     pinAnnotation = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pinID] autorelease]; 

    // Set the image 
    pinAnnotation.image = [UIImage imageNamed:@"TestIcon.png"]; 
0

Vous devez utiliser une sous-vue plutôt que la propriété d'image. Ce code permet de résoudre succssfully le problème pour moi:

UIImage * image = [UIImage imageNamed:@"blue_pin.png"]; 
    UIImageView *imageView = [[[UIImageView alloc] initWithImage:image] autorelease]; 
    [annView addSubview:imageView];