检测点击MK ANNOTATION视图标注气泡

时间:2014-09-05 11:58:47

标签: ios objective-c mkmapview

我在地图上有一个注释。当我选择注释时,我将显示带有自定义视图的标注气泡。现在,当我点击标注气泡时,我想转到新的视图控制器,但当我点击视图时,标注视图会消失。

-(void)mapView:(MKMapView *)mapView1 didSelectAnnotationView:(MKAnnotationView *)view
{
    NSLog(@"selected");
    if(![view.annotation isKindOfClass:[MKUserLocation class]])
    {
        CustomInfoWindow *calloutView =  [[[NSBundle mainBundle] 
        loadNibNamed:@"infoWindow"owner:self options:nil] objectAtIndex:0];
        CGRect calloutViewFrame = calloutView.frame;
        calloutViewFrame.origin = CGPointMake(-calloutViewFrame.size.width/2 + 15, -calloutViewFrame.size.height);
        calloutView.frame = calloutViewFrame;
        [calloutView.imagePlace.layer setBorderColor: [[UIColor orangeColor] CGColor]];
        [calloutView.imagePlace.layer setBorderWidth: 3.0];

        NSData* imageData = [[NSData alloc] initWithContentsOfURL:
        [NSURL  URLWithString:@"http://farm3.staticflickr.com/2926/14605349699_67a1d51b80.jpg"]];
        UIImage* image = [[UIImage alloc] initWithData:imageData];
        [calloutView.imagePlace setImage:image];
        [view addSubview:calloutView];
    }
}

-(void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view 
{
    for (UIView *subview in view.subviews ){
        [subview removeFromSuperview];
    }
}

2 个答案:

答案 0 :(得分:0)

我不是专家,但您可能想在视图中添加UITapGestureRecognizer。

使用以下代码:

UITapGestureRecognizer *tgr = [[UITabGestureRecognizer alloc] initWithTarget:self action:@selector(showNextView)];
calloutView.addGestureRecognizer(tgr);

- (void)showNextView {
    ...
}

答案 1 :(得分:0)

气泡没有逻辑,你需要使用标注配件。因此,使用MapView委托方法:

- (void)mapView:(MKMapView *)mv annotationView:(MKAnnotationView *)pin calloutAccessoryControlTapped:(UIControl *)control;

在viewForAnnotation委托方法中添加callout。例如:

UIButton *myDetailAccessoryButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
myDetailAccessoryButton.frame = CGRectMake(0, 0, 23, 23);
myDetailAccessoryButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
myDetailAccessoryButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
pinView.rightCalloutAccessoryView = myDetailAccessoryButton;

我希望我的回答可以帮到你。