ios-mapkit,自定义图像注释的奇怪行为
我编写了一些代码,用于在地图视图中显示带有自定义图像的注释。 当我将注释放入地图中时,我的地图视图委托实现了此方法来自定义注释:
- (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;
}
}
当我长按地图上的自定义注释时,问题是一个奇怪的行为: 显示图像更改和默认的红色引脚。
为什么会有这种行为?我怎样才能避免它?
I've written some code for showing annotations with custom images in a mapview.
My mapview delegate implements this method to customize annotation when they are put in the 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;
}
}
The problem is a strange behavior when I long click on a custom annotation on the map:
The image change and the default red pin is shown.
Why this behavior? And How can I avoid it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您想要为注释视图使用自定义图像时,请创建通用
MKAnnotationView
而不是MKPinAnnotationView
。MKPinAnnotationView
非常喜欢显示其默认图像,即图钉。稍微重新安排一下逻辑,以便对于
FONTANELLA
,它创建一个MKPinAnnotationView
,但对于其余的,它创建一个MKAnnotationView
。另外,您应该真正实现所有情况下的注释视图重用(最后一个 else 部分没有意义,因为如果出队不返回任何内容,则什么也不做 - 您可以这样做
返回 nil;
代替)。When you want to use a custom image for an annotation view, create a generic
MKAnnotationView
instead of anMKPinAnnotationView
.The
MKPinAnnotationView
really likes to display its default image which is a pin.Rearrange the logic a bit so that for
FONTANELLA
, it creates anMKPinAnnotationView
but for the rest anMKAnnotationView
.Also, you should really implement annotation view re-use for all cases (and the last
else
part doesn't make sense since nothing is done if the dequeue doesn't return anything--you could just doreturn nil;
instead)..h 文件内
.m 文件内
.m 类文件内
inside .h file
in .m file
inside .m class file