我正在制作一个使用 MKMapView 的应用程序。我添加了自定义图钉(带有图像)。现在当我放大然后缩小时,图钉变回默认值(红色)。
这是我的代码:
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
static NSString* SFAnnotationIdentifier = @"Kamera";
MKPinAnnotationView* pinView =
(MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:SFAnnotationIdentifier];
if (!pinView)
{
MKAnnotationView *annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:SFAnnotationIdentifier];
annotationView.canShowCallout = NO;
UIImage *flagImage = [UIImage imageNamed:@"pinModer.png"];
CGRect resizeRect;
resizeRect.size = flagImage.size;
resizeRect.size = CGSizeMake(40, 60);
resizeRect.origin = (CGPoint){0.0f, 0.0f};
UIGraphicsBeginImageContext(resizeRect.size);
[flagImage drawInRect:resizeRect];
UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
annotationView.image = resizedImage;
annotationView.opaque = NO;
UIImageView *sfIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"kameraNaprejModra.png"]];
annotationView.leftCalloutAccessoryView = sfIconView;
return annotationView;
}
return nil;
}
最佳答案
代码不处理 dequeue
返回非零 pinView
的情况(意味着它正在重新使用以前的注释 View )。
如果 pinView
不是 nil
,该方法在最后一行结束,为注释 View 返回 nil
。
当您返回 nil
时, map View 会绘制默认的注释 View ,即红色图钉。
像这样调整代码:
if (!pinView)
{
//no changes to code inside this if
//...
return annotationView;
}
//add an else part and return pinView instead of nil...
else
{
pinView.annotation = annotation;
}
return pinView;
关于iphone - 当我放大和缩小时,MKMapView 自定义引脚会发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13198166/