点击时,MKMapView不会在引脚上显示弹出窗口

时间:2013-09-19 08:38:29

标签: ios objective-c uiscrollview mkmapview

我有UIScrollView(灰色)附加UIView(白色)。在视图中,我附加了一个MKMapView,里面有一个引脚。

由于某些未知原因,引脚在点击时不显示弹出窗口。 这里有一些代码:

        UIView * whiteMapView = [[UIView alloc] initWithFrame:CGRectMake(x, y, w, h)];
        whiteMapView.backgroundColor = [UIColor whiteColor];
        whiteMapView.clipsToBounds = NO;
        whiteMapView.userInteractionEnabled = YES;
        [self.scrollView addSubview:whiteMapView];

        x = 10.0f;
        y = x;
        w = CGRectGetWidth(whiteMapView.frame) - (x * 2);
        h = w;

        self.mapView = [[MKMapView alloc] initWithFrame:CGRectMake(x, y, w, h)];
        self.mapView.delegate = self;
        self.mapView.scrollEnabled = NO;
        [whiteMapView addSubview:self.mapView];


        [self.mapView removeAnnotations:self.mapView.annotations];

        Shop * shop = [[Shop alloc] init];
        shop.name = self.userInfo[@"shopName"];
        shop.shopDescription = self.userInfo[@"goods_description"];
        shop.latitude = self.userInfo[@"lat"];
        shop.longitude = self.userInfo[@"lon"];

        MapPin * pin = [[MapPin alloc] initWithShop:shop];
        [self.mapView addAnnotation:pin];

这里是委托方法:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation
{
    if (annotation == mapView.userLocation)
    {
        return nil;
    }
    else
    {
        MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Pin"];
        pinView.userInteractionEnabled = YES;
        pinView.image = [UIImage imageNamed:@"pin_mappa_red"];
        pinView.canShowCallout = YES;
        pinView.animatesDrop = NO;

        UIButton *disclosureButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        disclosureButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        disclosureButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        [disclosureButton addTarget:self action:@selector(didPressPin:) forControlEvents:UIControlEventTouchUpInside];
        disclosureButton.tag = [[[(MapPin *)annotation shop] idshop] intValue];

        pinView.rightCalloutAccessoryView = disclosureButton;

        return pinView;
    }
}

在应用程序的另一部分,这段代码完美无缺。

想法?

enter image description here

2 个答案:

答案 0 :(得分:0)

尝试按以下方式更改viewForAnnotation

-(MKAnnotationView *)mapView:(MKMapView *)mapview viewForAnnotation:
(id <MKAnnotation>)annotation {

    MKPinAnnotationView *pinView = nil; 

    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    if(annotation) 
    {
        UIButton *disclosureButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

        UIImageView *imageInMap = [[UIImageView alloc]initWithImage: [UIImage imageNamed:@"net.png"]];

        [disclosureButton addTarget:self action:@selector(mapCallOutPressed) forControlEvents:UIControlEventTouchUpInside];


        static NSString *defaultPinID = @"com.invasivecode.pin";
        pinView = (MKPinAnnotationView *)[self.locationMapVIew dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
        if ( pinView == nil ) pinView = [[[MKPinAnnotationView alloc]
                                          initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];

        pinView.pinColor = MKPinAnnotationColorGreen; 

        pinView.rightCalloutAccessoryView = disclosureButton;
        pinView.leftCalloutAccessoryView = imageInMap;
        pinView.leftCalloutAccessoryView.frame = CGRectMake(0, 0, 35, 35);
        pinView.canShowCallout = YES;
        pinView.animatesDrop = YES;
    } 
    else {
        [self.locationMapVIew.userLocation setTitle:@"I am here"];
    }
    return pinView;
}

答案 1 :(得分:0)

检查此方法是否呼叫

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
NSLog("Pin called");
}

如果未调用此方法,则mapview或Annotation可能存在任何问题。