iOS: Pourquoi ne puis-je pas get mapkit pour afficher une image d'annotation personnalisée?

pensé que l'utilisation de ma propre image de broche personnalisée pour les annotations serait super facile.

Mais je n'ai jamais réussi à le faire fonctionner, et je ne sais pas pourquoi!

J'utilise simplement:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation { if([annotation isKindOfClass:[MKUserLocation class]]) return nil; NSSsortingng *annotationIdentifier = @"CustomViewAnnotation"; CustomAnnotationView * customAnnotationView = (CustomAnnotationView *) [self.mapview dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier]; if(!customAnnotationView) { customAnnotationView=[[CustomAnnotationView alloc] initWithAnnotationWithImage:annotation reuseIdentifier:annotationIdentifier annotationViewImage:[UIImage imageNamed:@"map_location_pin.png"]]; } customAnnotationView.image=[UIImage imageNamed:@"map_location_pin.png"]; customAnnotationView.canShowCallout= YES; return customAnnotationView; } 

J'ai supposé que cela devrait fonctionner, car un UIImage est ce qu'il manque, et j'ai le file .png dans mon projet.

Il n'utilise jamais mon image, mais seulement la broche rouge standard. Qu'est-ce que je fais mal ici?

Quelques pensées:

  1. Avez-vous défini votre delegate pour votre MKMapView ? Avez-vous mis un point d'arrêt ou NSLog ici pour vous assurer que votre viewForAnnotation est appelée?

  2. Vous n'avez pas partagé les CustomAnnotationView votre interface / implémentation CustomAnnotationView , mais vous n'avez pas besoin de sous- MKAnnotationView less que vous ne fassiez quelque chose de spécial. Si vous remplacez simplement l'image, vous pouvez simplement faire:

     - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation { if([annotation isKindOfClass:[MKUserLocation class]]) { return nil; } NSSsortingng *annotationIdentifier = @"CustomViewAnnotation"; MKAnnotationView* annotationView = [mapview dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier]; if (annotationView) { annotationView.annotation = annotation; } else { annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationIdentifier]; } annotationView.image = [UIImage imageNamed:@"map_location_pin.png"]; annotationView.canShowCallout= YES; return annotationView; } 

    Clairement, cependant, si vous faites quelque chose d'intéressant (comme une annotation personnalisée quand une broche est déplacée ou déposée), alors allez-y et ayez votre sous-class MKAnnotationView . Cela dépend simplement de ce que vous essayez de faire.

  3. Sans lien avec votre question initiale, je dirais:

    • que votre méthode viewForAnnotation utilise simplement la variable mapView locale plutôt que de referencer une propriété de class, self.mapView (bien qu'ils soient sans aucun doute les mêmes),

    • que vous envisagez de renommer votre méthode init personnalisée. Donc, au lieu de:

       customAnnotationView = [[CustomAnnotationView alloc] initWithAnnotationWithImage:annotation reuseIdentifier:annotationIdentifier annotationViewImage:[UIImage imageNamed:@"map_location_pin.png"]]; 

      que vous feriez quelque chose comme:

       customAnnotationView = [[CustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationIdentifier image:[UIImage imageNamed:@"map_location_pin.png"]]; 

Si vous rencontrez toujours des problèmes, partagez votre code CustomAnnotationView avec nous.

Je pense que c'est un support pour vous. Vous pouvez essayer ce code. Vous pouvez append mes files d'annotation .h et .m et faire quelques changements. Mon annotataion

mon annotation.h

  #import <Foundation/Foundation.h> #import <MapKit/MapKit.h> @interface MyAnnotation : NSObject<MKAnnotation> { CLLocationCoordinate2D coordinate; NSSsortingng* title; NSSsortingng* subtitle; UIImage* image; } @property (nonatomic, assign) CLLocationCoordinate2D coordinate; @property (nonatomic, copy) NSSsortingng* title; @property (nonatomic, copy) NSSsortingng* subtitle; @property (nonatomic, retain) UIImage* image; @end my annotation.m #import "MyAnnotation.h" @implementation MyAnnotation @synthesize title; @synthesize subtitle; @synthesize coordinate; @synthesize image; - (void)dealloc { [super dealloc]; self.title = nil; self.subtitle = nil; self.image=nil; } @end - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { //NSLog(@"welcome into the map view annotation"); // if it's the user location, just return nil. if ([annotation isKindOfClass:[MKUserLocation class]]) return nil; // try to dequeue an existing pin view first static NSSsortingng* AnnotationIdentifier = @"AnnotationIdentifier"; MKPinAnnotationView* pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier]autorelease]; //annotation.image = [UIImage imageNamed:@"shop-1.png"]; //[pinView setImage:[UIImage imageNamed:@"shop-1.png"]]; UIImageView *thumbnailImageView = [[UIImageView alloc] initWithImage:((MyAnnotation *)annotation).image]; CGRect newBounds = CGRectMake(0.0, 0.0, 32.0, 32.0); [thumbnailImageView setBounds:newBounds]; pinView.leftCalloutAccessoryView = thumbnailImageView; [thumbnailImageView release]; //UIImageView *profileIconView = [[UIImageView alloc] initWithImage:featuredDealcouponImage1]; // [pinView setImage:featuredDealcouponImage1]; pinView.animatesDrop=NO; pinView.canShowCallout=YES; pinView.pinColor=MKPinAnnotationColorGreen; UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; [rightButton setTitle:annotation.title forState:UIControlStateNormal]; [rightButton addTarget:self action:@selector(_annotation_btn_click:) forControlEvents:UIControlEventTouchUpInside]; pinView.rightCalloutAccessoryView = rightButton; return pinView; } 

Assurez-vous de sous-classr

 NSObject <MKAnnotation> 

et pas

 MKPinAnnotationView