2014-02-24 27 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方法之外创建注释视图的代码没有多大意义,因为不使用创建的视图。