我已经实现了一个地图应用程序,其中我有当前位置的显示引脚动画。当我点击引脚时,注释视图将打开。但是我想显示 注释视图,而不用点击pin.Is如果可能的话,那么请给我一些想法。如何显示注释视图,而无需在mapview中点击图钉?
在此先感谢。
我已经实现了一个地图应用程序,其中我有当前位置的显示引脚动画。当我点击引脚时,注释视图将打开。但是我想显示 注释视图,而不用点击pin.Is如果可能的话,那么请给我一些想法。如何显示注释视图,而无需在mapview中点击图钉?
在此先感谢。
我们可以在没有点击的情况下显示注释。使用下面的代码:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
static NSString *identifier = @"Pin";
MKPinAnnotationView *pin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
pin.canShowCallout = YES;
[pin setSelected:YES animated:NO];
return [pin autorelease];
}
你只需要找到您要选择并调用-setSelected:animated:
您MKAnnotationView实例。
例如,您可以循环您的的MKMapView像这样的注解:
for (YOURCLASSHERE *a in mapView.annotations) {
// Your current position (if shown) is an annotation as well. Thus check for the right class!
if ([a isKindOfClass:[YOURCLASSHERE class]]) {
// insert some smart if statement here, if you have multiple annotations!
[[mapView viewForAnnotation:a] setSelected:YES animated:YES];
}
}
YOURCLASSHERE是你的类,它实现MKAnnotation
协议。
当然,如果你已经知道你的annotationView,循环是多余的。
感谢它的工作 – JohnWhite 2010-12-07 09:28:35
试用:
看来,[mapView selectAnnotation:annotation animated:YES]
工作过。
希望这会有所帮助。
您的代码中存在内存泄漏,并且绝对不是实施委托方法的推荐方式。出于性能原因,您将需要重新使用现有的annotationView。 – 2010-12-07 09:19:10