• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - MKAnnotation 不显示相应的图标

[复制链接]
菜鸟教程小白 发表于 2022-12-13 07:27:41 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

在模拟器中,我的 map 看起来像这张图片。

enter image description here

但是,在我的设备中,我的 userLocation 附近有一些红点注释。 就像下图所示。 enter image description here

我猜这是由速度引起的某种运行时问题,是吗? 我该怎么做才能解决这个问题?

我正在使用两个类

ViewController.m

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

KMLParser.m

- (MKAnnotationView *)viewForAnnotationid <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;
}



Best Answer-推荐答案


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

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

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


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

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

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

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

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

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

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

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap