Je ne pense pas que je comprends cela correctement, j'ai ajouté l'annotation à la carte. Je peux le voir, la couleur est rouge. Cependant, je veux le changer en violet et je le veux comme un bouton. Donc, je le mets comme suit:MKAnnotation problèmes
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
//[self generateAnnotation];
CLLocationCoordinate2D aCoordinate;
aCoordinate.latitude = 32.224023;
aCoordinate.longitude = -110.965159;
MapAnnotation * an1 = [[[MapAnnotation alloc] initWithCoordinate: aCoordinate] autorelease];
an1.title = @"Party at Malloneys";
an1.subtitle = @"213 N. 4th Ave";
//[annotationArray addObject:an1];
[mapView addAnnotation:an1];
self.locationManager = [[[CLLocationManager alloc] init] autorelease];
self.locationManager.delegate=self;
self.locationManager.distanceFilter = kCLLocationAccuracyBest;
[self.locationManager startUpdatingLocation];
}
- (void) mapView:(MKMapView *) mapView
didAddAnnotationViews:(NSArray *) views
{
for (MKPinAnnotationView * mkaview in views)
{
mkaview.pinColor = MKPinAnnotationColorPurple;
UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
mkaview.rightCalloutAccessoryView = button;
}
}
- (MKAnnotationView *) mapView:(MKMapView *) mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
MKPinAnnotationView * annView = (MKPinAnnotationView*) [mapView dequeueReusableAnnotationViewWithIdentifier:@"an1"];
if (!annView)
{
annView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"an1"] autorelease];
annView.pinColor = MKPinAnnotationColorGreen;
annView.animatesDrop = YES;
annView.canShowCallout = YES;
}
else {
annView.annotation = annotation;
}
return annView;
}
Je ne comprends pas les deux délégués ci-dessus, viewForAnnotation est censé pour définir le point de vue du droit d'annotation? et la méthode déléguée didAddAnnotationViews est exécutée après le chargement de la vue? S'il vous plaît corriger ma compréhension si cela est faux. Et comment puis-je résoudre ce problème afin que je puisse changer de couleur et ajouter un bouton.
J'ai essayé de supprimer le didAddAnnotationViews et les pins sont toujours en rouge – aherlambang
La cause la plus probable est que le délégué de mapView n'est pas défini. Dans viewDidLoad, avant d'appeler addAnnotation, dites 'mapView.delegate = self;'. – Anna
il semble que viewForAnnotation n'est pas chargé/exécuté/tiré. J'ai juste essayé de mettre un NSLog à l'intérieur et ça n'a pas marché. Pourquoi??? – aherlambang