2010-12-07 38 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]工作過。

希望這會有所幫助。

相關問題