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

标签 ios annotations mkmapview mapkit

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

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

我只是在使用:

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

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

最佳答案

几个想法:

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

  2. 您还没有分享您的 CustomAnnotationView 接口(interface)/实现细节,但是您不需要子类化 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(尽管它们毫无疑问是一样的),

    • 您考虑重命名您的自定义初始化方法。所以不是:

      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 代码。

关于iOS : Why can't I get mapkit to display a custom annotation pin image?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15362002/

相关文章:

ios - 按下按钮时将 MKMapView 居中到我的位置

iphone - CLLocationCoordinate2D 制作行为

java - 如何将注释参数提取到外部文件中?

java - hibernate 多对一 : Duplicate entry 'album1' for key 'TITLE exception

ios - : clang: error: no such file or directory: 的 Travis-CI 错误

ios - 为什么要使用 resignFirstResponder()

java - 如何使用 spring 将所有属性读入数组?

cocoa-touch - 像 iOS7 Apple Maps 中的 MKMapView 夜间模式?

ios - Firebase 消息传递方法 swizzling 不起作用

ios - 使用 Core Location 在 iOS 上进行室内定位 - 不准确?