3

I am working with MKPinAnnotationView annotations pins over a MKMapView. Into the view that I am developing, there is a map centered on a pin. When I call the view the pin drops down and if I make a click over it, it shows the annotation information (canShowCallout = YES). How I can get this callout just when I open the view? Without making a click over the annotation pin.

Thanks for reading.

Edited:

I am using this code.

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

Edited:

Calling didAddAnnotationViews: instead of mapViewDidFinishLoadingMap: (as aBitObvious has commented), it works fine.

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

    id<MKAnnotation> myAnnotation = [self.mapView.annotations objectAtIndex:0];
    [self.mapView selectAnnotation:myAnnotation animated:YES];
}
Kampai
  • 22,848
  • 21
  • 95
  • 95
Daniel García Baena
  • 1,191
  • 4
  • 19
  • 33
  • 1
    I recently answered a [question](http://stackoverflow.com/questions/4151094/getting-the-title-to-show-up-when-map-loads-with-mkmap/4151381#4151381) like this. See if it works for you. –  Nov 19 '10 at 20:51
  • Thanks, it works great like this. The only shadow is that the pin shows the information before touching the map (I am animating the drop down). How can I do it after? – Daniel García Baena Nov 19 '10 at 21:10
  • Use performSelector:withObject:afterDelay. See [this answer](http://stackoverflow.com/questions/4083491/mapkit-how-to-detect-annotations-have-loaded/4083553#4083553). –  Nov 19 '10 at 21:15
  • Is there no way of knowing when the pin touches the map? – Daniel García Baena Nov 19 '10 at 21:34
  • Not that I know of unless you're drawing the animation yourself. –  Nov 19 '10 at 21:48
  • OK. The only problem now is that I can not give you the green tick, because you only has posted comments. Anyway, thank you so much. – Daniel García Baena Nov 20 '10 at 11:12

1 Answers1

3

Possible duplicate: How to trigger MKAnnotationView's callout view without touching the pin?

You want to call [mapView selectAnnotation:].

Community
  • 1
  • 1
GendoIkari
  • 11,734
  • 6
  • 62
  • 104
  • Sorry for the duplicate question. I am calling the selectAnnotation: method after adding the annotation, into the viewDidLoad method, but I still need make clic over the pin to show the information. – Daniel García Baena Nov 19 '10 at 19:25
  • Did you try calling selectAnnotation from (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView as the first answer suggests? – GendoIkari Nov 19 '10 at 19:36