2010-11-19 3 views
3

Я работаю с MKPinAnnotationView аннотациями контактов над MKMapView. В представлении, которое я разрабатываю, есть карта, сосредоточенная на булавке. Когда я вызываю вид, палец падает, и если я нажимаю на него, он показывает информацию аннотации (canShowCallout = YES). Как я могу получить эту выноску только тогда, когда я открою представление? Не нажимая на значок аннотации.Показаны данные Pin по умолчанию

Спасибо за чтение.

Отредактировано:

Я использую этот код.

- (void)viewDidLoad { 

    [super viewDidLoad]; 

    AddressAnnotation *annotation = [[[AddressAnnotation alloc] initWithCoordinate:self.currentAnnotationCoordinate] autorelease]; 
    [self.mapView addAnnotation:annotation]; 
    [self zoomCoordinate:self.currentAnnotationCoordinate]; 
} 

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation { 

    // If it's the user location, just return nil. 
    if ([annotation isKindOfClass:[MKUserLocation class]]) 
     return nil; 
    else { // Handles the other annotations. 
     // Try to dequeue an existing pin view first. 
     static NSString *AnnotationIdentifier = @"AnnotationIdentifier"; 
     MKPinAnnotationView *pinView = (MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier]; 

     if (!pinView) { 
      // If an existing pin view was not available, creates one. 
      MKPinAnnotationView *customPinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease]; 
      customPinView.animatesDrop = YES; 
      customPinView.canShowCallout = YES; 

      // Adds a detail disclosure button. 
      UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
      [rightButton addTarget:self action:@selector(showDetails:) forControlEvents:UIControlEventTouchUpInside]; 
      customPinView.rightCalloutAccessoryView = rightButton; 

      return customPinView; 
     } else 
      pinView.annotation = annotation; 
    } 

    return nil; 
} 


- (void)mapViewDidFinishLoadingMap:(MKMapView *)theMapView { 

    AddressAnnotation *annotation = [[[AddressAnnotation alloc] initWithCoordinate:self.currentAnnotationCoordinate] autorelease]; 

    for (id<MKAnnotation> currentAnnotation in mapView.annotations) {  
     if ([currentAnnotation isEqual:annotation]) { 
      [mapView selectAnnotation:currentAnnotation animated:FALSE]; 
     } 
    } 
} 

Отредактировано:

Вызов didAddAnnotationViews: вместо mapViewDidFinishLoadingMap: (как aBitObvious прокомментировал), он отлично работает.

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views { 

    id<MKAnnotation> myAnnotation = [self.mapView.annotations objectAtIndex:0]; 
    [self.mapView selectAnnotation:myAnnotation animated:YES]; 
} 
+1

Я недавно ответил на [вопрос] (http://stackoverflow.com/questions/4151094/getting-the-title-to-show-up-when-map-loads-with-mkmap/4151381#4151381) как это. Посмотрите, работает ли это для вас. – Anna

+0

Спасибо, он отлично работает. Единственная тень в том, что контакт показывает информацию, прежде чем касаться карты (я оживляю выпадающее меню). Как я могу это сделать после? –

+0

Использование performSelector: withObject: afterDelay. См. [Этот ответ] (http://stackoverflow.com/questions/4083491/mapkit-how-to-detect-annotations-have-loaded/4083553#4083553). – Anna

ответ

3

Возможный дубликат: How to trigger MKAnnotationView's callout view without touching the pin?

Вы хотите позвонить [mapView selectAnnotation:].

+0

Извините за дублированный вопрос. Я вызываю метод selectAnnotation: после добавления аннотации в метод viewDidLoad, но мне все еще нужно сделать клик над булавкой, чтобы показать информацию. –

+0

Пробовал ли вызывать selectAnnotation из (void) mapViewDidFinishLoadingMap: (MKMapView *) mapView, как предлагает первый ответ? – GendoIkari

+0

Да. Но он все еще не работает. –

Смежные вопросы