Question

I am trying to show callout on a pin by without clicking on the pin.In short, I want show the callout when the pins are getting placed on the map.

This is code I am using:

-(MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation         //  Method to handle all the annotations on map.
{

    if([annotation isKindOfClass: [MKUserLocation class]])
        return nil;

    static NSString *annotationID = @"MyAnnotation";

    MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationID] ;

    if (!pinView) {
        pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:annotationID];
        pinView.canShowCallout = YES;
        pinView.image = [UIImage imageNamed:@"map_pin.png"];

        //Setting Right call button
        pinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

        //Setting Left Call button
        self.favButton = [UIButton buttonWithType:UIButtonTypeCustom];
        self.favButton.frame = CGRectMake(0, 0, 23, 23);
        self.favButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        self.favButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        [self.favButton setImage:[UIImage imageNamed:@"favourite.png"] forState:UIControlStateNormal];
        pinView.leftCalloutAccessoryView = self.favButton;

        [self.mapView selectAnnotation:annotation animated:YES];
    }
    return pinView;

}

Any help would be appreciable.

Was it helpful?

Solution

Try with This :

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views{
    for (id<MKAnnotation> currentAnnotation in mapView.annotations) {
        if ([currentAnnotation isEqual:annotationToSelect]) {
            [mapView selectAnnotation:currentAnnotation animated:YES];
        }
    }
}

OR

EDIT : -(MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation // Method to handle all the annotations on map. {

if([annotation isKindOfClass: [MKUserLocation class]])
    return nil;

static NSString *annotationID = @"MyAnnotation";

MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationID] ;

if (!pinView) {
    pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:annotationID];
    pinView.canShowCallout = YES;
    pinView.image = [UIImage imageNamed:@"map_pin.png"];

    //Setting Right call button
    pinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

    //Setting Left Call button
    self.favButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.favButton.frame = CGRectMake(0, 0, 23, 23);
    self.favButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    self.favButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [self.favButton setImage:[UIImage imageNamed:@"favourite.png"] forState:UIControlStateNormal];
    pinView.leftCalloutAccessoryView = self.favButton;

            **[self performSelector:@selector(selectAnnotation:) withObject:annotation afterDelay:0.5];**

}
return pinView;

}

- (void)selectAnnotation:(id < MKAnnotation >)annotation
{
    [self.mapView selectAnnotation:annotation animated:NO];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top