Iphone – Displaying custom UIView in callout of pin(annotation) in map, using MapKit

iphonemapmkmapview

In my application i want to display custom view(with buttons) in the case of user touches annotation on map(standard pin).
i know that i can subclass MKAnnotationView and change the view of pin. but how can i change the look of Additional information view, that appears when user touches pin(by default it displays title and subtitle of MKAnnotation object).
how can i do this?

Thanks.

Best Answer

- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id )annotation
{
    MKPinAnnotationView *pinView = nil;
    static NSString *defaultPinID = @"ReusedPin";
    pinView = (MKPinAnnotationView*)[mVdequeueReusableAnnotationViewWithIdentifier:defaultPinID];
    if ( pinView == nil )
        pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
    if (((PinAnnotationView*)annotation).tag == 0 )
    {
        pinView.pinColor = MKPinAnnotationColorPurple;
    }
    else {
        pinView.pinColor = MKPinAnnotationColorRed;
    }
    pinView.canShowCallout = YES;
    pinView.animatesDrop = YES;
    UIImageView *pinImageView = [[UIImageView alloc] initWithFrame:CGRectMake(-5, 0, 34, 34)];
    UIImage *pinImage = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"icon" ofType:@"png"]];
    pinImageView.image = pinImage;
    [pinImage release]; 
    [pinView addSubview:pinImageView];
    [pinImageView release];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    btn.tag = ((PinAnnotationView*)annotation).tag;
    pinView.rightCalloutAccessoryView = btn;
    return pinView;
}

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
    if ( control.tag !=0) {
        ShowProviderDetailVC  *viewControlle = [[ShowProviderDetailVC alloc]initWithNibName:@"ShowProviderDetailVC" bundle:nil];
        viewControlle.lastViewName = @"SearchView";
        for (NSMutableDictionary* dict in globalLocArray) {
            if ( control.tag ==[[dict valueForKey:@"ID"] intValue] )
            {
                viewControlle.providerInfoDict = dict;
            }
     }
    [self.navigationController pushViewController:viewControlle animated:YES];
    [viewControlle release];
}
Related Topic