在iOS中显示按钮而不是地图上的图钉

时间:2014-03-12 07:59:49

标签: ios objective-c uibutton mkmapview mkannotation

我使用MKMapViewMKMapView上显示注释,但现在我想显示自定义注释而不是默认地图引脚。

我怎么能这样做??

2 个答案:

答案 0 :(得分:1)

试试这个:

- (MKAnnotationView *)mapView:(MKMapView *)mapView 
        viewForAnnotation:(id<MKAnnotation>)annotation {
        MKAnnotationView *annView = (MKAnnotationView *)[mapView 
                dequeueReusableAnnotationViewWithIdentifier: @"pin"];
        if (annView == nil) {
            annView = [[[MKAnnotationView alloc] initWithAnnotation:annotation 
                          reuseIdentifier:@"pin"] autorelease];

            annView.frame = CGRectMake(0, 0, 200, 50);

            UIButton *pinButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            pinButton.frame = CGRectMake(0, 0, 140, 28);
            pinButton.tag = 10;

            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] 
                initWithTarget:self action:@selector(handlePinButtonTap:)];
            tap.numberOfTapsRequired = 1;
            [pinButton addGestureRecognizer:tap];

            [annView addSubview:pinButton]; 
        }

        annView.annotation = annotation;

        UIButton *pb = (UIButton *)[annView viewWithTag:10];
        [pb setTitle:annotation.title forState:UIControlStateNormal];

        return annView;
}

- (void) handlePinButtonTap:(UITapGestureRecognizer *)gestureRecognizer {
    // Your tap action goes here
}

答案 1 :(得分:1)

  

我怎么能这样做??

停止使用MKPinAnnotationView,而是创建自己的MKAnnotationView子类。