iOS : Why can't I get mapkit to display a custom annotation pin image?

时光怂恿深爱的人放手 提交于 2019-12-06 09:27:23

A couple of thoughts:

  1. Did you define your delegate for your MKMapView? Have you put a breakpoint or NSLog in here to make sure your viewForAnnotation is getting called?

  2. You haven't shared your CustomAnnotationView interface/implementation details, but you don't need to subclass MKAnnotationView unless you're doing something special in there. If you're just replacing the image, you can just do:

    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
    {
        if([annotation isKindOfClass:[MKUserLocation class]]) {
            return nil;
        }
    
        NSString *annotationIdentifier = @"CustomViewAnnotation";
        MKAnnotationView* annotationView = [mapview dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
    
        if (annotationView) {
            annotationView.annotation = annotation;
        } else {
            annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationIdentifier];
        }
    
        annotationView.image = [UIImage imageNamed:@"map_location_pin.png"];
        annotationView.canShowCallout= YES;
    
        return annotationView;
    }
    

    Clearly, though, if you're doing something interesting (like custom annotation when a pin is dragged or dropped), then go ahead and have your CustomAnnotationView subclass MKAnnotationView. It just depends upon what you're trying to do.

  3. Unrelated to your original question, I'd suggest:

    • that your viewForAnnotation method just use the local mapView variable rather than referencing a class property, self.mapView (though they're undoubtedly the same),

    • that you contemplate renaming your custom init method. So instead of:

      customAnnotationView = [[CustomAnnotationView alloc] initWithAnnotationWithImage:annotation
                                                                       reuseIdentifier:annotationIdentifier 
                                                                   annotationViewImage:[UIImage imageNamed:@"map_location_pin.png"]];
      

      that you'd do something like:

      customAnnotationView = [[CustomAnnotationView alloc] initWithAnnotation:annotation
                                                              reuseIdentifier:annotationIdentifier 
                                                                        image:[UIImage imageNamed:@"map_location_pin.png"]];
      

If you're still having troubles, share some of your CustomAnnotationView code with us.

I think It's support to you.can you try this code.and you can add my annotation .h and .m files and do some changes My annotataion

my annotation.h

   #import <Foundation/Foundation.h>
    #import <MapKit/MapKit.h>

    @interface MyAnnotation : NSObject<MKAnnotation> {

        CLLocationCoordinate2D  coordinate;
        NSString*               title;
        NSString*               subtitle;
        UIImage*                image;
    }

    @property (nonatomic, assign)   CLLocationCoordinate2D  coordinate;
    @property (nonatomic, copy)     NSString*               title;
    @property (nonatomic, copy)     NSString*               subtitle;
    @property (nonatomic, retain)       UIImage*                image;

    @end


my annotation.m


        #import "MyAnnotation.h"


    @implementation MyAnnotation

    @synthesize title;
    @synthesize subtitle;
    @synthesize coordinate;
    @synthesize image;
    - (void)dealloc 
    {
        [super dealloc];
        self.title = nil;
        self.subtitle = nil;
        self.image=nil;
    }
    @end





       - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation

    {

        //NSLog(@"welcome into the map view annotation");

        // if it's the user location, just return nil.

        if ([annotation isKindOfClass:[MKUserLocation class]])

            return nil;

        // try to dequeue an existing pin view first
        static NSString* AnnotationIdentifier = @"AnnotationIdentifier";

        MKPinAnnotationView* pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier]autorelease];

        //annotation.image = [UIImage imageNamed:@"shop-1.png"];

        //[pinView setImage:[UIImage imageNamed:@"shop-1.png"]];

        UIImageView *thumbnailImageView = [[UIImageView alloc] initWithImage:((MyAnnotation *)annotation).image];
        CGRect newBounds = CGRectMake(0.0, 0.0, 32.0, 32.0);
        [thumbnailImageView setBounds:newBounds];
        pinView.leftCalloutAccessoryView = thumbnailImageView;
        [thumbnailImageView release];

        //UIImageView *profileIconView = [[UIImageView alloc] initWithImage:featuredDealcouponImage1];
        // [pinView setImage:featuredDealcouponImage1];
        pinView.animatesDrop=NO;

        pinView.canShowCallout=YES;

        pinView.pinColor=MKPinAnnotationColorGreen;

        UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

        [rightButton setTitle:annotation.title forState:UIControlStateNormal];

        [rightButton addTarget:self action:@selector(_annotation_btn_click:) forControlEvents:UIControlEventTouchUpInside];

        pinView.rightCalloutAccessoryView = rightButton;




        return pinView;

    }

Make sure you are subclassing

NSObject <MKAnnotation> 

and not

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