Creating Custom Call out View

拟墨画扇 提交于 2019-12-13 18:31:03

问题


I'm trying to add a view when I click in a pin, but appears only the title and subtitle and not the view that I did add.

Here is my code

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

and here I add the custom view. I put a breakpoint here and this function is called, but when I click in a pin, appearas only the title and subtitle..ignoring the view that I added

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


In viewForAnnotation, the code declares and initializes annotationView to nil and never actually instantiates it (alloc+init).

Therefore, the delegate method returns nil which tells the map view to "create the default view for this annotation". The default view for an annotation other than the user location is a red pin with a callout that only shows the title and subtitle.


However, if you just alloc+init a plain MKAnnotationView to fix it, the annotations will not be visible since by default an MKAnnotationView has no content. You would need to set its image or add some subview.

Instead, you could alloc+init an MKPinAnnotationView which by default will show a red pin.
You should also implement annotation view re-use by calling dequeueReusableAnnotationViewWithIdentifier to improve performance when there are lots of annotations:

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


This should fix the custom leftCalloutAccessoryView not appearing.
However, note what the documentation says for the leftCalloutAccessoryView (and rightCalloutAccessoryView):

The height of your view should be 32 pixels or less.

The custom view and label the code is creating are both more than 32 pixels high.


An unrelated comment regarding the loop that adds the annotations:
You do not need to call setRegion as you are adding each annotation.
Calling setRegion just tells the map view to display the indicated region.
That is not needed to add an annotation (the map doesn't have to be displaying the coordinate you are adding the annotation at).



来源:https://stackoverflow.com/questions/17863565/creating-custom-call-out-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!