【问题标题】:Creating Custom Call out View创建自定义调出视图
【发布时间】:2013-07-26 10:56:48
【问题描述】:

我试图在单击图钉时添加视图,但只显示标题和副标题,而不是我添加的视图。

这是我的代码

  -(void) inserirMapa {
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
    NSMutableArray *pins = [[NSMutableArray alloc]init]; //array de Annotations
    for(int iCnt = 0; iCnt < [_listaPins count]; iCnt++) {
        Pin *pin = (Pin *) [_listaPins objectAtIndex:iCnt];

        CLLocationCoordinate2D start;
        start.latitude = pin.place.latitude;
        start.longitude = pin.place.longitude;
        MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(start, 800, 800);
        [_myMapView setRegion:[_myMapView regionThatFits:region] animated:YES];

        // Add an annotation
        MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
        point.coordinate = start;
        point.title = @"Where am I?";
        point.subtitle = @"I'm here!!!";

        [_myMapView addAnnotation:point];

        [pins addObject:point];//adiciona a annotation no array
        [point release];
    }
    self.myAnnotations= [NSArray arrayWithArray:pins]; //diz que o array myAnnotations é igual ao array estacionamentos(que contem as annotations
        [pins release];
        [self configurarZoomDoMapaComLatitude:appDelegate.latitude eLongitude:appDelegate.longitude]; //configura o Zoom inicial do mapa


    [_myMapView addAnnotations:_myAnnotations];

    [_viewLoading setHidden:YES];
}

在这里我添加了自定义视图。我在这里放了一个断点并调用了这个函数,但是当我点击一个图钉时,只显示标题和副标题..忽略我添加的视图

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    MKAnnotationView* annotationView = nil;

    //your code

    UIView *vw = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    vw.backgroundColor = [UIColor redColor];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 50, 50)];
    label.numberOfLines = 4;
    label.text = @"hello\nhow are you\nfine";
    [vw addSubview:label];
    annotationView.leftCalloutAccessoryView = vw;
    return annotationView;

}

【问题讨论】:

    标签: ios objective-c mkmapview mapkit


    【解决方案1】:

    viewForAnnotation 中,代码将annotationView 声明并初始化为nil,但从未实际实例化它(alloc+init)。

    因此,委托方法返回nil,它告诉地图视图“为此注释创建默认视图”。除用户位置外,注释的默认视图是带有标注的红色图钉,仅显示标题和副标题。


    但是,如果您只是分配 + 初始化一个普通的 MKAnnotationView 来修复它,注释将不可见,因为默认情况下 MKAnnotationView 没有内容。您需要设置它的image 或添加一些子视图。

    相反,您可以分配 + 初始化 MKPinAnnotationView,默认情况下会显示一个红色图钉。
    您还应该通过调用dequeueReusableAnnotationViewWithIdentifier 来实现注释视图重用,以在有大量注释时提高性能:

    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
    {
        static NSString *reuseId = @"ann";
    
        MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[mapView 
            dequeueReusableAnnotationViewWithIdentifier:reuseId];
        if (annotationView == nil)
        {
            annotationView = [[MKPinAnnotationView alloc] 
                                     initWithAnnotation:annotation 
                                        reuseIdentifier:@"test"];
            annotationView.canShowCallout = YES;  //set to YES, default is NO
    
            //your code
    
            UIView *vw = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
            vw.backgroundColor = [UIColor redColor];
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 50, 50)];
            label.numberOfLines = 4;
            label.text = @"hello\nhow are you\nfine";
            [vw addSubview:label];
            annotationView.leftCalloutAccessoryView = vw;
        }
        else
        {
            //Update view's annotation reference 
            //because we are re-using view that may have
            //been previously used for another annotation...
            annotationView.annotation = annotation;
        }
    
        return annotationView;
    }
    


    这应该可以解决自定义 leftCalloutAccessoryView 不出现的问题。
    但是,请注意文档对leftCalloutAccessoryView(和rightCalloutAccessoryView)的说明:

    视图的高度应为 32 像素或更小。

    代码创建的自定义视图和标签的高度均超过 32 像素。


    关于添加注释的循环的不相关评论:
    在添加每个注释时,您无需调用 setRegion
    调用 setRegion 只是告诉地图视图显示指定区域。
    这不需要添加注释(地图不必显示您要添加注释的坐标)。

    【讨论】:

    • 谢谢,它有效。但是我怎样才能创建一个更大的视图呢?我需要在单击 pin 时显示一个 100 x100 的带有 ImageViews、标签和按钮的视图。有可能吗?
    • 我确实发现了这个example code 使用自定义注释视图的示例代码。
    • 内置的标注视图不像你看到的那样可定制。唯一的方法是禁用它(canShowCallout=NO),然后在 didSelectAnnotationView 中,将自定义标注视图添加到注释视图本身(这是一种方法)。您找到的示例代码似乎是一种流行的选择。
    • 是的,我的问题是为什么 acessoryview 没有出现……而您回答得很好,因此我确实将您的答案标记为正确。但我真正想要的是自定义标注视图。谢谢帮忙!
    猜你喜欢
    • 2011-12-20
    • 1970-01-01
    • 1970-01-01
    • 2011-05-24
    • 2012-07-25
    • 2015-06-12
    • 2022-08-02
    相关资源
    最近更新 更多