我正在创建自定义注释,并且正在尝试使用 dequeueReusableAnnotation。
引脚之间的区别在于用于引脚图像的 png。
我创建了 myAnnotation 类,并在创建注释时使用了此代码:
if([category isEqualToString:@"anti-social-behaviour"]){
point.annotationImg=@"A.png";
}
else
if([category isEqualToString:@"burglary"]){
point.annotationImg=@"B.png";
}
else....
现在在 viewForAnnotation 中:
- (MKAnnotationView *)mapView:(MKMapView *)aMapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if ([annotation class] == MKUserLocation.class) {
return nil;
}
static NSString *identifier = @"myPin";
MKPinAnnotationView *pinView = nil;
pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (pinView == nil)
{
pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
pinView.canShowCallout = YES;
pinView.image = [UIImage imageNamed:[(Annotation*)annotation annotationImg]];
}
return pinView;
}
我知道我必须以某种方式使用标识符,但我没有弄清楚。
现在的问题是我第一次加载引脚时很好,第二次加载图像变得困惑。
有什么建议吗?
最佳答案
我建议在 if/else block 之外实现以下代码行,因为当您将 MKPinAnnotationView 出队时会发生什么,如果它返回一个 View ,那么您只是返回仍然引用旧图像的出队 View 。
所以你需要在 if/else 之后设置图像,它应该类似于
if (pinView == nil)
{
pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
pinView.canShowCallout = YES;
}
pinView.image = [UIImage imageNamed:[(Annotation*)annotation annotationImg]];
关于ios - viewForAnnotation 中的注解标识符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17371626/