How do I show multiple custom annotations (pin & left icon) loaded via plist?

后端 未结 2 1037
[愿得一人]
[愿得一人] 2021-02-11 09:27

will this code when working show custom pins and logos or just 1 pin with custom logos? please see plist below, only error is Local declaration of mapView hides instance variabl

2条回答
  •  再見小時候
    2021-02-11 10:14

    (Posting a separate answer for the slightly modified question. The original question was how to change the annotation callout image based on some property of the annotation object. The modified question is why the annotations are not appearing at all based on the latest code and how to fix the warning in viewForAnnotation.)

    The only obvious problem with the latest code in the question is that the viewDidLoad method is spelled wrong which prevents that code from getting called at all. Please change this:

    - (void)viewDidload  //the lowercase "l" is not correct, must be uppercase
    

    to this:

    - (void)viewDidLoad
    


    A few other things to check just in case:

    • Plist file should really be like xxxyyyzzz
    • Make sure the image files like pin-green.png, etc have been added to the project and are named exactly that way (uppercase/lowercase matters on the device)

    Next, to fix the compiler warning in viewForAnnotation, change the name of the mapView parameter to something else so it's different from the mapView ivar in the view controller. Following example changes it to aMapView (two places marked with ^^^^^^):

    -(MKAnnotationView *)mapView:(MKMapView *)aMapView viewForAnnotation:(id)annotation
                                              ^^^^^^^^
    {  
        if ([annotation isKindOfClass:[MyAnnotation class]])
        {
            static NSString *reuseId = @"customAnn";
    
            MKAnnotationView *customAnnotationView = [aMapView dequeueReusableAnnotationViewWithIdentifier:reuseId];
                                                      ^^^^^^^^
            if (customAnnotationView == nil)
            {
                customAnnotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId] autorelease];
                UIImage *pinImage = [UIImage imageNamed:@"pin-green.png"];
                [customAnnotationView setImage:pinImage];
                customAnnotationView.canShowCallout = YES;
                UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
                customAnnotationView.rightCalloutAccessoryView = rightButton;
            }
    
            NSString *iconFilename = @"";
            MyAnnotation *myAnn = (MyAnnotation *)annotation;
            if ([myAnn.stationIdKey isEqualToString:@"BP"])
                iconFilename = @"bp-logo.png";
            else
                if ([myAnn.stationIdKey isEqualToString:@"Caltex"])
                    iconFilename = @"caltex.png";
                else
                    if ([myAnn.stationIdKey isEqualToString:@"Shell"])
                        iconFilename = @"shell.png";
            UIImageView *leftIconView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:iconFilename]] autorelease];
            customAnnotationView.leftCalloutAccessoryView = leftIconView;
    
            customAnnotationView.annotation = annotation;
    
            return customAnnotationView; 
        }
    
        return nil; 
    }
    


    This is how it looks (your pin and callout images may be different):

    Green Pins

    Shell Callout

    Caltex Callout

    BP Callout

    If you want the pins themselves to have different images instead of their callouts, then in viewForAnnotation, call setImage instead of setting the leftCalloutAccessoryView.


    Another minor, unrelated, thing is that you don't need to call mapView setDelegate inside the for-loop. You only need to call it once before the for-loop (and if you've already connected the map view's delegate to File's Owner in the xib, you don't need to do it in code at all).

提交回复
热议问题