jjzjj

iphone - MKMapView 自定义图像不工作

coder 2024-01-26 原文

尝试遵循本教程:http://www.switchonthecode.com/tutorials/building-an-earthquake-monitor-for-iphone-using-mapkit

我的图标显示在所有正确的位置(和正确的数字)并且图像也显示了。 (但它始终是相同的图像。那是因为条件 if(self.marker.type == "nzpost"不起作用,因为类型始终为 null(initWithAnnotation() 中标记对象的所有属性也是如此) . 由于某种原因,属性都是空的。

注解 View 界面:

@interface PayMarkerAnnotationView : MKAnnotationView {
    PaymentMarker *marker;
}

@property (strong, nonatomic) IBOutlet PaymentMarker *marker;

@end

带注释的初始化:(这里是 self.marker.type 应该有值的地方。

- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {
    if(self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = [UIColor clearColor];
        NSString* imageName = [[NSString alloc] init];

        if([self.marker.type isEqualToString:@"nzpost"]) {
            imageName = @"icon-map-post.png";
        } else {
            imageName = @"icon-map-incharge.png";
        }
        self.image = [UIImage imageNamed:imageName];
    }
    return self;
}

map View :

- (MKAnnotationView *)mapView:(MKMapView *)lmapView viewForAnnotation:(id <MKAnnotation>)annotation {
    PayMarkerAnnotationView *markerView = (PayMarkerAnnotationView *)[lmapView dequeueReusableAnnotationViewWithIdentifier:@"markerView"];
    if(markerView == nil) {
        markerView = [[PayMarkerAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"markerView"];
    }
    markerView.annotation = annotation;
    return markerView;
}

添加我的标记

-(void)renderPaymentMarkers
{
    [self.map addAnnotations: nzPostMarkers];
    [self.map addAnnotations: inChargeMarkers];
}

Marker class:

@interface PaymentMarker : NSObject <MKAnnotation> {
    NSString* type;
    NSString* description;
    float latitude;
    float longitude;}

@property (nonatomic) NSString* description;
@property (nonatomic) NSString* type;
@property (nonatomic) float latitude;
@property (nonatomic) float longitude;

//MKAnnotation
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

@end

最佳答案

永远不会设置 PayMarkerAnnotationView 中的 marker 变量。

(我不确定您为什么还要将它的属性设为 IBOutlet。)

访问前需要在initWithAnnotation方法中设置:

if (self = [super initWithAnnotation...
    //set marker equal to the annotation this view is being created for...
    marker = annotation;

    self.backgroundColor = [UIColor clearColor];
    ...

但是,您首先不需要 marker 变量。
MKAnnotationView 已经有一个 annotation 属性,它会在您调用 super initWithAnnotation 时设置。

所以在你的 initWithAnnotation 中,不要这样:

if([self.marker.type isEqualToString:@"nzpost"]) {

你可以这样做:

PaymentMarker *pm = (PaymentMarker *)self.annotation;
if([pm.type isEqualToString:@"nzpost"]) {

这应该可以解决主要问题。


另一个潜在的问题是,在 viewForAnnotation 中,您正在直接更新 View 的 annotation 属性,以防 View 被另一个注释重新使用。

这很好(虽然它应该在 else block 中完成)但问题是 View 的 image 不会在 annotation 已更新。

因此,如果使用该 View 的先前注释的类型为“nzpost”,但当前注释为其他类型,则该 View 仍将显示先前注释类型的图像。

要解决这个问题,这里有两种可能的解决方案:

  • 为每种类型使用单独的 reuseIdentifier(因此有两个 ID:“post”和“incharge”)。这样,只有具有匹配图像的 View 才会重新用于当前注释。
  • 覆盖 PayMarkerAnnotationView 中的 setAnnotation: 并更新 annotationimage 属性。我更喜欢这个解决方案。


其他一些完全不相关的项目......

这一行:

NSString* imageName = [[NSString alloc] init];

没有意义,因为稍后的代码只是用新值覆盖了 imageName。所以分配的内存被放弃了。相反,只需声明 imageName:

NSString* imageName;

这些属性:

@property (nonatomic) NSString* description;
@property (nonatomic) NSString* type;

应该用 copy 属性声明(编译器应该会提示这个):

@property (nonatomic, copy) NSString* description;
@property (nonatomic, copy) NSString* type;

您可能还想更改 description 属性的名称,因为您正在覆盖 NSObject 提供的同名属性(除非这是您的意图)。

关于iphone - MKMapView 自定义图像不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12081527/

有关iphone - MKMapView 自定义图像不工作的更多相关文章

  1. ruby - Facter::Util::Uptime:Module 的未定义方法 get_uptime (NoMethodError) - 2

    我正在尝试设置一个puppet节点,但ruby​​gems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由ruby​​gems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby

  2. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  3. ruby-on-rails - 'compass watch' 是如何工作的/它是如何与 rails 一起使用的 - 2

    我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t

  4. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  5. ruby-on-rails - form_for 中不在模型中的自定义字段 - 2

    我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢

  6. ruby - 主要 :Object when running build from sublime 的未定义方法 `require_relative' - 2

    我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby​​1.9+ 关于ruby-主要:Objectwhenrun

  7. ruby - 无法让 RSpec 工作—— 'require' : cannot load such file - 2

    我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳

  8. ruby-on-rails - rspec should have_select ('cars' , :options => ['volvo' , 'saab' ] 不工作 - 2

    关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion在首页我有:汽车:VolvoSaabMercedesAudistatic_pages_spec.rb中的测试代码:it"shouldhavetherightselect"dovisithome_pathit{shouldhave_select('cars',:options=>['volvo','saab','mercedes','audi'])}end响应是rspec./spec/request

  9. ruby-on-rails - s3_direct_upload 在生产服务器中不工作 - 2

    在Rails4.0.2中,我使用s3_direct_upload和aws-sdkgems直接为s3存储桶上传文件。在开发环境中它工作正常,但在生产环境中它会抛出如下错误,ActionView::Template::Error(noimplicitconversionofnilintoString)在View中,create_cv_url,:id=>"s3_uploader",:key=>"cv_uploads/{unique_id}/${filename}",:key_starts_with=>"cv_uploads/",:callback_param=>"cv[direct_uplo

  10. ruby - 在 Ruby 中有条件地定义函数 - 2

    我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin

随机推荐