在mapview中向注释气泡添加按钮

时间:2011-11-15 09:35:15

标签: iphone

以下是我在地图视图中添加注释的代码。此代码在viewdidload方法

中调用
-(void)displayMYMap
{
MKCoordinateRegion region = mapView.region; 
MKCoordinateSpan span; 
span.latitudeDelta=0.2; 
span.longitudeDelta=0.2; 

addAnnotation = [[[AddressAnnotation alloc] initWithCoordinate:mapCenter title:firstName SubTitle:lastName Recordid:passedRecordID]autorelease];

[mapView addAnnotation:addAnnotation];
region.span=span; 
region.center=mapCenter; 
}

我正在尝试为注释气泡添加一个按钮。我在尝试将按钮添加到注释视图时遇到异常。

UIButton *informationButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure ];
addAnnotation.rightCalloutAccessoryView = informationButton;

这是我为添加按钮而编写的代码。我无法找出异常的原因。

2 个答案:

答案 0 :(得分:4)

使用Annotation的委托方法,

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    static NSString *identifier = @"RoutePinAnnotation";
        MKPinAnnotationView* pinView = [[[MKPinAnnotationView alloc]
                                         initWithAnnotation:annotation reuseIdentifier:identifier] autorelease];
        pinView.animatesDrop=YES;
        pinView.canShowCallout=YES;
        if ([(UICCircleAnnotation *)annotation annotationType] == UICCircleAnnotationTypeOther) {
            pinView.pinColor = MKPinAnnotationColorPurple;
        } else if ([(UICCircleAnnotation *)annotation annotationType] == UICCircleAnnotationTypeContact) {
            pinView.pinColor = MKPinAnnotationColorRed;
        }

        UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        [rightButton setTitle:annotation.title forState:UIControlStateNormal];
        [rightButton addTarget:self
                        action:@selector(buttonMethod:)
              forControlEvents:UIControlEventTouchUpInside];
        pinView.rightCalloutAccessoryView = rightButton;

        UIImageView *profileIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"profile.png"]];
        pinView.leftCalloutAccessoryView = profileIconView;
        [profileIconView release];

        return pinView;
}

答案 1 :(得分:0)

在注释视图的委托方法中,

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    static NSString *identifier = @"RoutePinAnnotation";
        MKPinAnnotationView* pinView = [[[MKPinAnnotationView alloc]
                                         initWithAnnotation:annotation reuseIdentifier:identifier] autorelease];
        pinView.animatesDrop=YES;
        pinView.canShowCallout=YES;
        if ([(UICCircleAnnotation *)annotation annotationType] == UICCircleAnnotationTypeOther) {
            pinView.pinColor = MKPinAnnotationColorPurple;
        } else if ([(UICCircleAnnotation *)annotation annotationType] == UICCircleAnnotationTypeContact) {
            pinView.pinColor = MKPinAnnotationColorRed;
        }

        pinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];


        return pinView;
}

您将在注释气泡上获得披露按钮。