创建自定义呼出视图

时间:2013-07-25 16:21:42

标签: ios objective-c mkmapview mapkit

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

这是我的代码

  -(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;

}

1 个答案:

答案 0 :(得分:7)

viewForAnnotation中,代码声明并初始化annotationViewnil并且从不实际实例化它(alloc + init)。

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


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

相反,您可以分配+ init 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只会告诉地图视图显示指定的区域 添加注释不需要这样(地图不必显示您要添加注释的坐标)。