ios - MKAnnotation不显示相应的图标

标签 ios mkannotation

我在 map 中使用了自定义图标作为注释。 大约有200个引脚。

在模拟器中,我的 map 如下图所示。

enter image description here

但是,在我的设备中,有一些红点注释靠近我的用户位置。 就像下图所示的那样。 enter image description here

我猜测这是由速度引起的某种运行时问题,是吗? 我该如何解决这个问题?

我正在使用两个类

ViewController.m

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id    
      <MKAnnotation>)annotation
{
   return [kmlParser viewForAnnotation:annotation];
}

KMLParser.m

- (MKAnnotationView *)viewForAnnotation:(id <MKAnnotation>)point
{
// Find the KMLPlacemark object that owns this point and get
// the view from it.
for (KMLPlacemark *placemark in _placemarks) {
    if ([placemark point] == point)
        return [placemark annotationView];
}
return nil;
}

- (MKAnnotationView *)annotationView
{
if (!annotationView) {
    id <MKAnnotation> annotation = [self point];
    if (annotation) {
        MKPinAnnotationView *pin =
        [[MKPinAnnotationView alloc] initWithAnnotation:annotation   
        reuseIdentifier:nil];
        UIImage * img = [UIImage imageNamed:@"WaterStation"] ;
        CGRect resizeRect;
        resizeRect.size.height = 40;
        resizeRect.size.width = 40;
        resizeRect.origin = (CGPoint){0.0f, 0.0f};
        UIGraphicsBeginImageContext(resizeRect.size);
        [img drawInRect:resizeRect];
        UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        pin.image = resizedImage;
        pin.canShowCallout = YES;
        pin.animatesDrop = YES;
        annotationView = pin;
    }
}
return annotationView;
}

最佳答案

不要创建 MKPinAnnotationView,而是创建一个普通的 MKAnnotationView

MKPinAnnotationView 子类往往会忽略 image 属性,因为它被设计为仅显示标准的红色、绿色、紫色引脚(通过 pinColor) > 属性(property))。

当您切换到 MKAnnotationView 时,您还必须注释掉 animatesDrop 行,因为该属性特定于 MKPinAnnotationView


@omz 在评论中提到的一个单独的点不会导致图像显示问题,但会影响性能:

您不需要每次需要注释 View 时都以编程方式创建调整大小的图像:

  • 正如 @omz 所说,您只需将已调整大小的图像添加到您的包中即可

    pin.image = [UIImage imageNamed:@"WaterStationResized"];
    

    而不是所有 CG 的东西(因为你的图像基本上是一个常数)。这将是最好、最简单的改进。

  • 如果您知道所有注释都将使用相同的图像,您还可以消除 for 循环,只在此处创建并返回注释 View 。请记住,按原样,每次 map View 请求每个注释的 View 时,for 循环都会执行。
  • 代码看起来有点复杂,因此不清楚 if (!annotationView) 是否实际上是 YES。您可能想确认这一点,因为这似乎是您的缓存机制。
  • 您还可以使用 dequeueReusableAnnotationViewWithIdentifier: 让 map View 为您完成缓存,而不是自己进行缓存。

关于ios - MKAnnotation不显示相应的图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22990474/

相关文章:

ios - 获取 NSGenericException 原因为 : '*** Collection <NSConcreteHashTable: 0x282c34140> was mutated while being enumerated.'

javascript - iOS 上的 JQ UI 可拖动 : initiating dragging in taphold-handler

objective-c - 如何访问呈现模态视图的 UIViewController 类/类名?

ios - 如何混淆项目IOS中的所有代码

ios - 如何正确折叠我的单元格?

ios5 - MKPointAnnotation : title always visible. 可能吗?

ios - 将图钉放在 map 上时在注释中显示地址

ios - 在容器 View 上添加 ViewController 时, TableView 动画无法正常工作?

objective-c - 获取另一个注解周围的所有注解

swift - 协议(protocol)只能用作通用约束