Ich habe eine NSArray
von benutzerdefinierten MKAnnotation
, ich muss eine rote PIN-Farbe für die erste/letzte Annotation setzen, sonst setzen Sie einen grünen Pin. Das Programm verhält sich nicht wie ich will, die grünen Pins sind jeweils mit zwei verschiedenen Annotationen auf zufällige Weise verbunden, sind nicht mit dem ersten und dem letzten verbunden, wie ich will.Set MKPinAnnotationColorRed nur für die erste und letzte MKAnnotation auf MKMapView
Also, das ist, was ich in meinem Controller so mache:
- (void)viewDidLoad
{
[super viewDidLoad];
//load set a coords from file etc...
//self.coordinates is the array of annotations
[self.myMap addAnnotations:self.coordinates];
}
dann im viewForAnnotation: Rückruf:
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{
NSString *ident = @"MY_IDENTIFIER";
MKPinAnnotationView *annView=(MKPinAnnotationView *)[self.myMap dequeueReusableAnnotationViewWithIdentifier:ident];
if(self.myMap.userLocation==annotation){
return nil;
}
if(annView == nil){
annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:ident];
annView.animatesDrop=TRUE;
annView.canShowCallout = YES;
annView.rightCalloutAccessoryView=[UIButton buttonWithType:UIButtonTypeDetailDisclosure];
annView.calloutOffset = CGPointMake(-5, 5);
int currIdxAnn=[self.myMap.annotations indexOfObject:annotation];
int lastIdxAnn=[self.myMap.annotations indexOfObject:[self.myMap.annotations lastObject]];
/*
if (currIdxAnn==0 || currIdxAnn==lastIdxAnn) {
annView.pinColor=MKPinAnnotationColorRed;
}else {
annView.pinColor=MKPinAnnotationColorGreen;
}*/
CustomAnnotation *an=(CustomAnnotation *)annotation;
if (an.tag==98||an.tag==99) {
annView.pinColor=MKPinAnnotationColorGreen;
}else {
annView.pinColor=MKPinAnnotationColorRed;
}
}
return annView;
}
Es scheint, dass das Verfahren addAnnotations:
nicht funktioniert, wie ich glaube, , lädt die Annotationen wahrscheinlich mit einer anderen Reihenfolge als das Array. Ist es möglich?
Ich habe auch versucht, einen ähnlichen Prozess in didAddAnnotationViews:
aber ohne gute Ergebnisse zu machen.
Einige Hinweise? Danke.
PS: als ich fertig mit dem Schreiben dieser Frage ich fand this Antwort, und scheint meine Theorie zu bestätigen. Jemand hat bereits ein ähnliches Problem festgestellt?
EDIT: Nach einigen Tests wurde mir klar, dass der beste Weg zu erreichen, was ich will, ist auf einen Tag meine ersten/letzten Anmerkungen gesetzt:
CustomAnnotation *first=[self.coordinates objectAtIndex:0];
first.tag=98;
CustomAnnotation *last=[self.coordinates objectAtIndex:[self.coordinates indexOfObject:[self.coordinates lastObject]]];
last.tag=99;
Dann, wie Sie ich habe sehen können leicht modifizierte die viewForAnnotation
, um die Anmerkung zu verstehen, war die eine, die ich suchte. Dann wurde der Trick Aufruf nur die Funktion, dass die Anmerkungen in einem Hintergrund-Thread fügt hinzu, wie:
[self performSelectorInBackground:@selector(addAllAnnot) withObject:nil];
-(void)addAllAnnot{
[self.myMap addAnnotations:self.coordinates];
}
Das ist für mich nach einer Woche der Tests gearbeitet, wenn jemand eine bessere Idee hat, wird es geschätzt.
Ich denke, Sie sollten 'MKAnnotationView' anstelle von' MKPinAnnotationView' verwenden. –
@nevanking Kannst du es besser erklären? Ich muss MKAnnotationView verwenden. – Mat
@nevanking ..sorry ich meinte, ich brauche 'MKPinAnnotationView' zu verwenden. – Mat