2010-12-07 40 views

回答

-2

我们可以在没有点击的情况下显示注释。使用下面的代码:

- (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]; 
} 
+0

您的代码中存在内存泄漏,并且绝对不是实施委托方法的推荐方式。出于性能原因,您将需要重新使用现有的annotationView。 – 2010-12-07 09:19:10

2

你只需要找到您要选择并调用-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,循环是多余的。

+0

感谢它的工作 – JohnWhite 2010-12-07 09:28:35

4

试用:

看来,[mapView selectAnnotation:annotation animated:YES]工作过。

希望这会有所帮助。

相关问题