2014-02-24 58 views
2

我有一個存儲緯度和經度值的數據庫。我想遍歷列表並在地圖上顯示它們。我寫了一個條件,說如果類型是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; 
} 

我能夠正確顯示引腳,但顏色無法正確顯示。我在這裏做錯了什麼?

回答

2

一個顏色屬性添加到BasicMapAnnotation類。查詢屬性viewForAnnotation正確初始化pinColor。順便說一下,用於在viewForAnnotation方法之外創建註釋視圖的代碼沒有多大意義,因爲不使用創建的視圖。