【发布时间】:2021-02-11 00:56:21
【问题描述】:
我在地图图钉下方添加标签以显示图钉的一些细节。我这样做是通过添加 UILabel 作为 annotationView 的子视图并将其行数设置为 0 以便在同一标签上显示多行。我遇到了一个问题,地图最初加载了正确的注释,我可以在地图图钉的标签中看到正确的数据,但是当我导航到另一个屏幕然后返回地图时,地图图钉是都在正确的位置,但它们下面的标签不再在标签中显示正确的数据,它们都被混淆了。地图图钉开始显示属于其他地图图钉的数据。此外,如果我将地图移动一点,然后回到同一个引脚,也会发生同样的事情。
我在 viewForAnnotation 方法中给标签添加数据:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
// Return the view marker different for editing items ie draggable undragrable.
if ([annotation isKindOfClass:[PSMapAnnotation class]] == YES) {
PSMapAnnotation *senderAnnotation = (PSMapAnnotation *)annotation;
PSMapAnnotationView *annotationView = (PSMapAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:[senderAnnotation getPinKey]];
if (annotationView == nil) {
annotationView = [[PSMapAnnotationView alloc]
initWithAnnotation:senderAnnotation
reuseIdentifier:[senderAnnotation getPinKey]];
// THIS LINE RETURNS A STRING ARRAY CONTAINING LABEL DATA
NSMutableArray *taggedAnswers = [self _getTaggedAnswersForMarkerLabel:annotationView];
annotationView.enabled = YES;
annotationView.canShowCallout = NO;
UILabel *annotationLabel = [[UILabel alloc] init];
annotationLabel.text = @"";
annotationLabel.numberOfLines = 0;
annotationLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:14.0];
annotationLabel.textColor = [UIColor blackColor];
annotationLabel.textAlignment = NSTextAlignmentCenter;
// LOOP THROUGH STRING ARRAY AND ADD ALL STRINGS TO ANNOTATION LABEL
for (NSString* taggedAnswer in taggedAnswers)
{
NSString *textToShow = [NSString stringWithFormat: @"%@\n", taggedAnswer];
annotationLabel.text = [annotationLabel.text stringByAppendingString: textToShow];
}
[annotationLabel sizeToFit];
annotationLabel.center = CGPointMake(annotationView.center.x, annotationView.center.y);
annotationView.myLabel = annotationLabel;
[annotationView addSubview:annotationLabel];
[annotationView setAnnotation:annotation];
}
else {
[annotationView setAnnotation:annotation];
}
return annotationView;
【问题讨论】:
标签: ios objective-c mapkit mapkitannotation