ios - 无法在 MKMapView 中显示多个注释图钉

标签 ios ios7 mkmapview mapkit mkannotationview

我有一个存储纬度和经度值的数据库。我想遍历列表并将它们显示在 map 上。我编写了一个条件,表示如果类型是 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;
}

我能够正确显示引脚,但颜色显示不正确。我在这里做错了什么?

最佳答案

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

关于ios - 无法在 MKMapView 中显示多个注释图钉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21982726/

相关文章:

ios - Widget 与WatchOS2 之间如何通信?

exception - NSObject(NSObject)didNotRecognizeSelector崩溃的应用程序,报告缺少某些符号

ios - 检测 mkoverlay 上的触摸

ios - 从 map 注释标注的过渡

iphone - 更改 MKMapView 中的 MKMapType 并保留自定义 pinImage 用于注释

ios - misaligned_stack_error_是什么意思?

IOS popviewcontroller去除导航栏

iphone - gpus_ReturnGuiltyForHardwareRestart 崩溃

iphone - iOS 7 UIImagePickerController 有黑色预览

ios - MFMailComposeViewController 忽略 iOS 7 中的一些 UIAppearance 协议(protocol)