2010-06-05 5 views

Répondre

0

Vous devez d'abord créer la classe MKAnnotation Protocol (ici, la classe MyAnnotation implémente le MKAnnotation Protocol). Cette classe doit inclure lat, long, titre, sous-titre et aussi ce que vous voulez.

Deuxièmement, votre contrôleur de vue, où vous souhaitez afficher la broche et vous mettrez en œuvre ce code,

AnnObj = [[MyAnnotation alloc] init]; 

AnnObj.latitude = [[latitude objectAtIndex:storyIndex] floatValue]; 

AnnObj.longitude = [[longitude objectAtIndex:storyIndex] floatValue]; 

MKCoordinateRegion region; 

region.center = location; 

MKCoordinateSpan span; 

region.center.latitude = [[latitude objectAtIndex:storyIndex] floatValue]; 

region.center.longitude = [[longitude objectAtIndex:storyIndex] floatValue]; 

span.latitudeDelta = 0.0005; 

span.longitudeDelta = 0.0005; 

region.span = span; 

[mapview setRegion:region animated:TRUE]; 

AnnObj.title = titleString; 

AnnObj.subTitle = subTitleString; 

NSString * titleString = [[buildingNames objectAtIndex:storyIndex] stringByReplacingOccurrencesOfString:@"\n\t" withString:@""]; 

[eventPoints addObject:AnnObj]; 

[mapview addAnnotations:eventPoints]; 

Third, Implement the MKAnnotation delegate method, 

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

    if (lmapView.userLocation == annotation){ 

     return nil; 
     } 

    MKAnnotationView* myCusAnn = (MKAnnotationView*)[lmapView dequeueReusableAnnotationViewWithIdentifier:@"eventview"]; 

    if(myCusAnn == nil) 
    { 
     myCusAnn = [[[ MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"eventview"] autorelease]; 

    } 

    myCusAnn.canShowCallout = YES; 

    [myCusAnn setEnabled:YES]; 

    return myCusAnn; 

    } 

J'espère que cela vous aidera.

1
  1. Créer une nouvelle classe qui implémente le protocole MKAnnotation. Cela tiendra le lat/long, et aura également un titre et une description qui sera affiché si vous sélectionnez l'annotation après qu'elle a été rendue sur votre carte.
  2. Le contrôleur de la vue qui affichera la carte devra implémenter la méthode - - (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation; du protocole MKMapViewDelegate. Le code dans cette méthode ressemblera à quelque chose comme le code en bas (excuses pour le mauvais formatage, je ne pouvais pas aller ici ou en bas).

  3. Puis à un moment donné dans votre code de contrôleur, vous devrez appeler quelque chose le long des lignes de [self.mapView addAnnotation: annotation]; où l'annotation est une instance de votre classe d'annotation personnalisée créée à l'étape 1, avec l'ensemble lat/long, etc.

  4. Enfin, pour que la méthode viewForAnnotation soit appelée correctement et qu'elle soit facile à ignorer dans le constructeur d'interface, assurez-vous que vous avez défini la sortie déléguée de MKMapView comme votre contrôleur (qui implémente le protocole MKMapViewDelegate.)

    -(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation { 
    
        static NSString *AnnotationViewIdentifier = @"annotationViewIdentifier"; 
    
        MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier: AnnotationViewIdentifier]; 
    
        if (annotationView == nil) { 
        annotationView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier: AnnotationViewIdentifier] autorelease]; 
    
    // This is all optional and depends on your requirements 
        annotationView.animatesDrop = NO; 
        annotationView.canShowCallout = YES; 
        annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
        annotationView.enabled = YES; 
        annotationView.pinColor = MKPinAnnotationColorGreen; 
        } 
        return annotationView; 
    } 
    
+0

Merci pour la réponse .. –

+0

Peut-être que vous pourriez éditer votre réponse afin que le code soit mieux mis en évidence. – testing

Questions connexes