草庐IT

ios - 无法在 MKMapView 中显示多个注释图钉

coder 2024-01-17 原文

我有一个存储纬度和经度值的数据库。我想遍历列表并在 map 上显示它们。我写了一个条件,如果类型是 atm,那么注释颜色应该是绿色,如果是 branch,它应该显示红色。

if ([myNewArrayElement isEqualToString:@"BRANCH"]) {                   
    self.customAnnotation = [[BasicMapAnnotation alloc] initWithLatitude:[[record valueForKey:@"lat"]floatValue] andLongitude:[[record valueForKey:@"lon"]floatValue]] ;
    NSLog(@"Current coordinates%f%f",[[record valueForKey:@"lat"]floatValue],[[record valueForKey:@"lon"]floatValue]);
    mPinColor = @"PURPLE";

    self.customAnnotation.title = record.type;
    MKPinAnnotationView * annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:self.customAnnotation
                                                              reuseIdentifier:@"CustomAnnotation"] ;
    annotationView.canShowCallout = YES;
    mPinColor = @"";
    NSLog(@"Its Basic");
    annotationView.pinColor = MKPinAnnotationColorGreen;

    [self.mapView addAnnotation:self.customAnnotation];
    self.mapView.delegate = self;
}
else if ([myNewArrayElement isEqualToString:@"ATM"]) {
    self.normalAnnotation = [[BasicMapAnnotation alloc] initWithLatitude:[[record valueForKey:@"lat"]floatValue] andLongitude:[[record valueForKey:@"lon"]floatValue]] ;
    mPinColor = @"GREEN";
    self.normalAnnotation.title = record.type;
    MKPinAnnotationView *annotationView = [[MKPinAnnotationView alloc]initWithAnnotation:self.normalAnnotation
                                                               reuseIdentifier:@"NormalAnnotation"] ;
   annotationView.canShowCallout = YES;
   mPinColor = @"";
   NSLog(@"Its Basic");
   annotationView.pinColor = MKPinAnnotationColorGreen;
   [self.mapView addAnnotation:self.normalAnnotation];

}

我的 viewForAnnotation 是

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {

    NSLog(@"Length of mpincolor%d",mPinColor.length);
    MKPinAnnotationView *annotationView;
    if ([mPinColor isEqualToString:@"PURPLE"]) {
        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomAnnotation"] ;
        annotationView.canShowCallout = NO;
        NSLog(@"Its custom");
        annotationView.pinColor = MKPinAnnotationColorPurple;
        return annotationView;
    }
    else if([mPinColor isEqualToString:@"GREEN"]) {

       annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation
                                                             reuseIdentifier:@"NormalAnnotation"] ;
       annotationView.canShowCallout = YES;
       mPinColor = @"";
       NSLog(@"Its Basic");
       annotationView.pinColor = MKPinAnnotationColorGreen;

       return annotationView;
    }
    return nil;
}

我能够正确显示图钉,但颜色显示不正确。我在这里做错了什么?

最佳答案

BasicMapAnnotation 类添加一个color 属性。在 viewForAnnotation 中查询该属性以正确初始化 pinColor。顺便说一句,您用于在 viewForAnnotation 方法之外创建注释 View 的代码没有多大意义,因为创建的 View 未被使用。

关于ios - 无法在 MKMapView 中显示多个注释图钉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21982726/

有关ios - 无法在 MKMapView 中显示多个注释图钉的更多相关文章

  1. 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""-

  2. ruby-on-rails - Rails 3 中的多个路由文件 - 2

    Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题

  3. ruby-on-rails - Rails 编辑表单不显示嵌套项 - 2

    我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib

  4. ruby-on-rails - 在 Ruby 中循环遍历多个数组 - 2

    我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代

  5. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  6. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  7. ruby-on-rails - 无法使用 Rails 3.2 创建插件? - 2

    我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby​​1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在

  8. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  9. ruby-on-rails - 无法在centos上安装therubyracer(V8和GCC出错) - 2

    我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e

  10. 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) 最佳

随机推荐