使用MapKit在地图中的pin(注释)的标注中显示自定义UIView

时间:2011-01-17 11:43:08

标签: iphone map mkmapview

在我的应用程序中,我希望在用户触摸地图上的注释(标准引脚)时显示自定义视图(带按钮)。 我知道我可以继承MKAnnotationView并更改pin的视图。但是如何更改用户触摸图钉时显示的附加信息视图的外观(默认情况下显示MKAnnotation对象的标题和副标题)。 我怎么能这样做?

感谢。

2 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:2)