2

认为使用我自己的自定义 pin 图像进行注释会非常容易。

但我一直无法让它工作,我不知道为什么!

我只是在使用:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    if([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    NSString *annotationIdentifier = @"CustomViewAnnotation";
    CustomAnnotationView * customAnnotationView = (CustomAnnotationView *) [self.mapview dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
    if(!customAnnotationView)
    {
        customAnnotationView=[[CustomAnnotationView alloc] initWithAnnotationWithImage:annotation
                                                                       reuseIdentifier:annotationIdentifier 
                                                                   annotationViewImage:[UIImage imageNamed:@"map_location_pin.png"]];
    }

    customAnnotationView.image=[UIImage imageNamed:@"map_location_pin.png"];

    customAnnotationView.canShowCallout= YES;

    return customAnnotationView;
}

我认为这应该可以工作,因为 UIImage 是它想要的,而且我的项目中确实有 .png 文件。

它从不使用我的图像,而只是使用标准的红色别针。我在这里做错了什么?

4

3 回答 3

6

几个想法:

  1. delegate为你定义了你的MKMapView?您是否设置了断点或NSLog在此处以确保您viewForAnnotation被调用?

  2. 你还没有分享你的CustomAnnotationView接口/实现细节,但你不需要子类化MKAnnotationView,除非你在那里做一些特别的事情。如果你只是替换图像,你可以这样做:

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

    但是,很明显,如果您正在做一些有趣的事情(例如拖放或拖放引脚时的自定义注释),那么请继续使用您的CustomAnnotationViewsubclass MKAnnotationView。这仅取决于您要做什么。

  3. 与您的原始问题无关,我建议:

    • 您的viewForAnnotation方法只使用局部mapView变量而不是引用类属性self.mapView(尽管它们无疑是相同的),

    • 您考虑重命名自定义 init 方法。所以而不是:

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

      你会做这样的事情:

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

如果您仍然遇到问题,请CustomAnnotationView与我们分享您的一些代码。

于 2013-03-12T15:30:46.467 回答
2

我认为它对你的支持。你可以试试这个代码。你可以添加我的注释 .h 和 .m 文件并做一些更改我的注释

我的注释.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;

    }
于 2013-03-12T13:13:31.450 回答
0

确保你是子类化

NSObject <MKAnnotation> 

并不是

MKPinAnnotationView
于 2013-03-12T14:11:09.363 回答