无法在MKMapView中显示多个注释引脚

时间:2014-02-24 08:42:37

标签: ios ios7 mkmapview mapkit mkannotationview

我有一个存储纬度和经度值的数据库。我想遍历列表并在地图上显示它们。我写了一个条件,说如果类型是atm那么注释颜色应该是绿色,如果它是branch,它应该显示为红色。

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 个答案:

答案 0 :(得分:2)

颜色属性添加到 BasicMapAnnotation 类。在 viewForAnnotation 中查询该属性以正确初始化 pinColor 。顺便说一句,用于在viewForAnnotation方法之外创建注释视图的代码并没有太大意义,因为创建的视图没有被使用。

答案 1 :(得分:1)