我在地图中使用自定义图标作为注释。 大约有200个针脚。
在模拟器中,我的地图看起来像这张图片。
但是,在我的设备中,有一些红点注释靠近我的userLocation。
就像下图所示。
我猜这是一个由速度引起的某种运行时问题的问题,是吗? 我该怎么做才能解决这个问题?
我正在使用两个班级
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;
}
答案 0 :(得分:4)
创建普通MKPinAnnotationView
。
MKAnnotationView
MKPinAnnotationView
子类倾向于忽略image
属性,因为它仅用于显示标准的红色,绿色,紫色引脚(通过pinColor
属性)。
当您切换到MKAnnotationView
时,您还必须注释掉animatesDrop
行,因为该属性特定于MKPinAnnotationView
。
<小时/> @omz在评论中提到了一个不会导致图像显示问题但会影响性能的单独一点:
每次需要注释视图时,您无需以编程方式创建重新调整大小的图像:
正如@omz所说,您只需将已调整大小的图片添加到您的包中即可
pin.image = [UIImage imageNamed:@"WaterStationResized"];
而不是所有CG的东西(因为你的图像基本上是一个常数)。这将是最好和最简单的改进。
for
循环,只需在那里创建并返回注释视图。请记住,for
循环每次执行时都会执行地图视图要求每个注释的视图。if (!annotationView)
是否实际上是YES
。您可能想要确认这一点,因为这似乎是您的缓存机制。dequeueReusableAnnotationViewWithIdentifier:
让地图视图为您执行此操作,而不是自己进行缓存。