2013-05-12 14 views
5

Mein MKPointAnnotation mit diesem Code sollte benutzerdefinierte sein:Warum ist meine MKPointAnnotation nicht benutzerdefiniert?

-(MKPointAnnotation*)setAnnotation: (NSString*) title atLocation:(CLLocationCoordinate2D)Location withImage:(UIImage*) LocationImage{ 
    Pin = [[MKPointAnnotation alloc] init]; 
    Pin.title = title; 
    [Pin setCoordinate:Location]; 
    [self mapView:mapView viewForAnnotation:Pin].annotation = Pin; 
    return Pin; 
} 



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

     MKPinAnnotationView* pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"]; 


     if(!pinView){ 
      pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"CustomPinAnnotationView"]; 
      pinView.animatesDrop = YES; 
      pinView.canShowCallout = YES; 
      pinView.enabled = YES; 
      UIButton *PicButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
      [PicButton addTarget:self action:@selector(showLocationPicture) forControlEvents:UIControlEventTouchUpInside];\ 
      pinView.rightCalloutAccessoryView = PicButton; 
     } 
     else{ 
      pinView.annotation = annotation; 
     } 
    return pinView; 
} 

Doch aus irgendeinem Grund wird die Pin immer noch Standard, kann mir jemand helfen hier? Danke

Antwort

10

Sie sollten viewForAnnotation nicht selbst anrufen. Sie sollten Ihrer Kartenansicht nur Anmerkungen hinzufügen, die dann bestimmen, welche zu einem bestimmten Zeitpunkt sichtbar sind, und viewForAnnotation für Sie aufrufen. Also:

  • Da verschiedene Anmerkungen verschiedene Bilder haben können, wollen Sie wirklich MKPointAnnotation Unterklasse und geben ihm eine Eigenschaft des imageName (beachten Sie, nicht die UIImage selbst, sondern ein Name oder eine Kennung, die viewForAnnotation wird in der Lage sein, verwenden, um die UIImage selbst zu erstellen):

    @interface MyAnnotation : MKPointAnnotation 
    @property(nonatomic, strong) NSString *imageName; 
    @end 
    
    @implementation MyAnnotation 
    @end 
    
  • eine Anmerkung hinzufügen (keine Anmerkungsansicht), um Ihre mapview, zB:

    - (id<annotation>)addAnnotationWithTitle:(NSString *)title coordinate:(CLLocationCoordinate2D)coordinate imageName:(NSString *)imageName 
    { 
        MyAnnotation *annotation = [[MyAnnotation alloc] init]; 
        annotation.title = title; 
        annotation.coordinate = coordinate; 
        annotation.imageName = imageName; 
        [mapView addAnnotation:annotation]; 
        return annotation; 
    } 
    

    Hinweis, ich würde nicht vorschlagen Pin eine Klasse Ivar/Eigenschaft, und ich würde CamelCase für Variablennamen verwenden (beginnend mit Kleinbuchstaben), würde ich den Methodennamen von setAnnotation zu etwas wie addAnnotationWithTitle, etc. ändern

  • Stellen Sie sicher, dass Ihr Controller als Delegierter der mapView festgelegt wurde;

  • Die viewForAnnotation wird für Sie aufgerufen (wenn die Annotation sichtbar ist). Es sollte wohl so etwas wie sein:

    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation 
    { 
        if ([annotation isKindOfClass:[MKUserLocation class]]) 
         return nil; 
    
        if ([annotation isKindOfClass:[MyAnnotation class]]) 
        { 
         MyAnnotation *customAnnotation = annotation; 
    
         static NSString *annotationIdentifier = @"MyAnnotation"; 
         MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier]; 
         if (!annotationView) 
         { 
          annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationIdentifier]; 
          annotationView.canShowCallout = YES; 
          annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
         } 
         else 
         { 
          annotationView.annotation = annotation; 
         } 
    
         annotationView.image = [UIImage imageNamed:customAnnotation.imageName]; 
    
         return annotationView; 
        } 
    
        return nil; 
    } 
    

    Hinweis, die animatesDrop ist eine MKPinAnnotationView Option, die Sie nicht mit benutzerdefinierten Beschriftungsansichten tun können. Aber es ist einfach, eine zu implementieren, wenn Sie es wollen (siehe How can I create a custom "pin-drop" animation using MKAnnotationView?). Ich würde vorschlagen, dass Sie zuerst die grundlegende Anmerkungsansicht angehen und dann später die benutzerdefinierte Anmerkungsansichts-Drop-Animation betrachten.

+0

Aber wie passe ich dann die Einstellungen für die Anmerkung an? weil ich im Moment Dinge wie pinView.rightCalloutAccessoryView = PicButton; aber das funktioniert auch nicht –

+0

@BlackMagic Siehe meine überarbeitete Antwort mit vollständiger Code-Beispiel. – Rob

+1

Vielen Dank, das war genau das, was ich brauchte. –