iOS版:为什么我不能得到mapkit显示自定义注释销的形象? [英] iOS : Why can't I get mapkit to display a custom annotation pin image?

查看:143
本文介绍了iOS版:为什么我不能得到mapkit显示自定义注释销的形象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想通,使用自己的自定义针图像注释是超级容易。

figured that using my own custom pin image for annotations would be super easy.

但我从来没有能够得到它的工作,我不知道为什么!

But I have never been able to get it to work, and I have no idea why!

我简单地使用:

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

I assumed that this should work, as a UIImage is what it is wanting, and I do have the .png file in my project.

它绝不会使用我的形象,只是标准的红脚。我在做什么错在这里?

It never uses my image, but just the standard red pin. What am I doing wrong here?

推荐答案

一对夫妇的想法:


  1. 你定义你的委托的MKMapView ?你在这里放一个断点或的NSLog ,以确保您的 viewForAnnotation 获取调用?

  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?

您还没有分享你的 CustomAnnotationView 接口/实现细节,但你并不需要继承 MKAnnotationView ,除非你正在做的东西在里面特别。如果你刚刚更换的图像,你可以这样做:

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

由此可见,虽然,如果你(当引脚被拖动或丢弃,如自定义注释)做一些有趣的事情,然后继续前进,有你的 CustomAnnotationView MKAnnotationView 。这只是取决于你想要做的事。

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.

无关的你原来的问题,我建议:

Unrelated to your original question, I'd suggest:


  • 这是你的 viewForAnnotation 方法只使用本地图形页面,而不是变量引用类属性, self.mapView (虽然他们无疑是相同的),

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

这是你考虑重命名您的自定义init方法。因此,而不是:

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"]];


如果你还有问题,分享一些你的 CustomAnnotationView $ C $的c。与我们联系。

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

这篇关于iOS版:为什么我不能得到mapkit显示自定义注释销的形象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆