我编写了一些代码,用于在 map View 中显示带有自定义图像的注释。 我的 mapview 委托(delegate)实现此方法以在将注释放入 map 时自定义注释:
- (MKAnnotationView *) mapView:(MKMapView *) mapView viewForAnnotation:(id<MKAnnotation>) annotation {
if ([annotation isKindOfClass:[Station class]]) {
Station *current = (Station *)annotation;
MKPinAnnotationView *customPinview = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
if([[current type] compare:FONTANELLA]==NSOrderedSame)
customPinview.pinColor = MKPinAnnotationColorPurple;
else{
int test=current.bici;
if(test==0)
customPinview.image = [UIImage imageNamed:@"bicimir.png"];
else if(test<4)
customPinview.image = [UIImage imageNamed:@"bicimi.png"];
else if(test>=4)
customPinview.image = [UIImage imageNamed:@"bicimig.png"];
}
customPinview.animatesDrop = NO;
customPinview.canShowCallout = YES;
return customPinview;
}
else{
NSString *identifier=@"MyLocation";
MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [_mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
return annotationView;
}
当我长按 map 上的自定义注释时,问题是一个奇怪的行为: 图像发生变化并显示默认的红色图钉。
为什么会有这种行为?我该如何避免呢?
最佳答案
当您想为注释 View 使用自定义图像时,创建一个通用的 MKAnnotationView
而不是 MKPinAnnotationView
。
MKPinAnnotationView
非常喜欢显示其默认图像,即图钉。
稍微重新安排一下逻辑,以便为 FONTANELLA
创建一个 MKPinAnnotationView
,但为其余部分创建一个 MKAnnotationView
。
此外,您应该真正实现对所有情况的注释 View 重用(并且最后的 else
部分没有意义,因为如果出队不返回任何内容,则什么都不做——你可以只做 return nil;
代替)。
关于ios-mapkit,带有自定义图像注释的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8098257/