【问题标题】:iOS : Why can't I get mapkit to display a custom annotation pin image?iOS:为什么我不能让 mapkit 显示自定义注释图钉图像?
【发布时间】:2013-02-28 00:16:58
【问题描述】:

认为使用我自己的自定义图钉图像进行注释会非常简单。

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

我只是在使用:

- (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 文件。

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

【问题讨论】:

标签: ios annotations mkmapview mapkit


【解决方案1】:

一些想法:

  1. 您是否为您的MKMapView 定义了您的delegate?您是否在此处设置了断点或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;
    }
    

    不过,很明显,如果您正在做一些有趣的事情(例如在拖放或拖放图钉时自定义注释),那么请继续使用您的 CustomAnnotationView 子类 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 代码。

【讨论】:

    【解决方案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;
    
        }
    

    【讨论】:

      【解决方案3】:

      确保你是子类化

      NSObject <MKAnnotation> 
      

      而不是

      MKPinAnnotationView
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-12-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-27
        • 1970-01-01
        • 2021-11-09
        相关资源
        最近更新 更多