我有一個存儲緯度和經度值的數據庫。我想遍歷列表並在地圖上顯示它們。我寫了一個條件,說如果類型是atm
,那麼註釋顏色應該是綠色,如果它是branch
它應該顯示爲紅色。無法在MKMapView中顯示多個註釋引腳
if ([myNewArrayElement isEqualToString:@"BRANCH"]) {
self.customAnnotation = [[BasicMapAnnotation alloc] initWithLatitude:[[record valueForKey:@"lat"]floatValue] andLongitude:[[record valueForKey:@"lon"]floatValue]] ;
NSLog(@"Current coordinates%f%f",[[record valueForKey:@"lat"]floatValue],[[record valueForKey:@"lon"]floatValue]);
mPinColor = @"PURPLE";
self.customAnnotation.title = record.type;
MKPinAnnotationView * annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:self.customAnnotation
reuseIdentifier:@"CustomAnnotation"] ;
annotationView.canShowCallout = YES;
mPinColor = @"";
NSLog(@"Its Basic");
annotationView.pinColor = MKPinAnnotationColorGreen;
[self.mapView addAnnotation:self.customAnnotation];
self.mapView.delegate = self;
}
else if ([myNewArrayElement isEqualToString:@"ATM"]) {
self.normalAnnotation = [[BasicMapAnnotation alloc] initWithLatitude:[[record valueForKey:@"lat"]floatValue] andLongitude:[[record valueForKey:@"lon"]floatValue]] ;
mPinColor = @"GREEN";
self.normalAnnotation.title = record.type;
MKPinAnnotationView *annotationView = [[MKPinAnnotationView alloc]initWithAnnotation:self.normalAnnotation
reuseIdentifier:@"NormalAnnotation"] ;
annotationView.canShowCallout = YES;
mPinColor = @"";
NSLog(@"Its Basic");
annotationView.pinColor = MKPinAnnotationColorGreen;
[self.mapView addAnnotation:self.normalAnnotation];
}
我viewForAnnotation是
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
NSLog(@"Length of mpincolor%d",mPinColor.length);
MKPinAnnotationView *annotationView;
if ([mPinColor isEqualToString:@"PURPLE"]) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomAnnotation"] ;
annotationView.canShowCallout = NO;
NSLog(@"Its custom");
annotationView.pinColor = MKPinAnnotationColorPurple;
return annotationView;
}
else if([mPinColor isEqualToString:@"GREEN"]) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:@"NormalAnnotation"] ;
annotationView.canShowCallout = YES;
mPinColor = @"";
NSLog(@"Its Basic");
annotationView.pinColor = MKPinAnnotationColorGreen;
return annotationView;
}
return nil;
}
我能夠正確顯示引腳,但顏色無法正確顯示。我在這裏做錯了什麼?