创建自定义呼出视图

sha*_*sad 1 objective-c mapkit mkmapview ios

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

这是我的代码

  -(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];
}
Run Code Online (Sandbox Code Playgroud)

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

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

}
Run Code Online (Sandbox Code Playgroud)

小智 7

viewForAnnotation中,代码声明并初始化annotationViewnil实际上从未对其进行实例化(ALLOC + INIT).

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


但是,如果您只是MKAnnotationView为了修复它而分配+ init ,则注释将不可见,因为默认情况下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;
}
Run Code Online (Sandbox Code Playgroud)


这应该可以解决leftCalloutAccessoryView不显示的自定义问题.
但是,请注意文档对leftCalloutAccessoryView(和rightCalloutAccessoryView)所说的内容:

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

代码创建的自定义视图和标签都高于32像素.


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