草庐IT

ios - 在 map View 上重新添加注释以更新其标题

coder 2024-01-10 原文

正如您在下面看到的,我已经以基本方式模拟了我的问题。我有一个定时器,它会定期调用一个方法。在那个方法中,我创建了一个 switch-case 条件来模拟我的想法。

一旦在 map 上添加了图钉,然后再次读取(图钉不断下降)它们。

我想添加我的 pin,然后只更改代表天气值的标题。

- (IBAction)playBtn:(id)sender {

     timer = [NSTimer scheduledTimerWithTimeInterval:(4.0) target:self selector:@selector(control) userInfo:nil repeats:YES];

}

-(void)control{
    NSMutableArray *annotationArray = [[[NSMutableArray alloc] init] autorelease];

    switch (value%2) {
        case 0:
        {

    // Create some annotations
    Annotation *annotation = nil;

    annotation = [[Annotation alloc] init];
    annotation.coordinate = CLLocationCoordinate2DMake(29.7161,-95.3906);
    annotation.color = RGB(13, 0, 182);
    annotation.title = @"17";
    [annotationArray addObject:annotation];
    [annotation release];

    annotation = [[Annotation alloc] init];
    annotation.coordinate = CLLocationCoordinate2DMake(30.168456,-95.504480);
    annotation.color = RGB(0, 182, 146);
    annotation.title = @"16";
    [annotationArray addObject:annotation];
    [annotation release];


    // Center map
    //self.mapView.visibleMapRect = [self makeMapRectWithAnnotations:annotationArray];

    // Add to map
    //[self.mapView addAnnotations:annotationArray];
        }
            break;
        case 1:
        {
            // Create some annotations
            Annotation *annotation = nil;

            annotation = [[Annotation alloc] init];
            annotation.coordinate = CLLocationCoordinate2DMake(29.7161,-95.3906);
            annotation.color = RGB(13, 0, 182);
            annotation.title = @"27";
            [annotationArray addObject:annotation];
            [annotation release];

            annotation = [[Annotation alloc] init];
            annotation.coordinate = CLLocationCoordinate2DMake(30.168456,-95.504480);
            annotation.color = RGB(0, 182, 146);
            annotation.title = @"25";
            [annotationArray addObject:annotation];
            [annotation release];

        }
            break;
    }
    [self.mapView addAnnotations:annotationArray];
    [mapView setNeedsDisplay];
    value++;

最佳答案

如果您想更新已添加到 map 的注释的属性,使用新属性再次添加它(无需先删除旧的)只需创建并添加另一个注释在同一位置(具有新属性)。

如您所见,调用 removeAnnotation 然后调用 addAnnotation 会导致闪烁和另一个放置动画。

相反,您必须获取对现有 注释的引用,然后更新其属性。

如果它只是一个注释,您可以保留对它的类级 ivar 引用,并在值更改时使用该引用更新属性。

如果您需要在不同的时间更新不同的注释,一个简单的方法是在 map View 的 annotations 数组中搜索您要更新的注释。

这要求您在注释类中有一个属性,该属性对于每个注释都是唯一的,并且(理想情况下)对每个注释保持不变。换句话说:如果您要更新注释的 title,请不要使用 title 作为“唯一”注释标识符。

相反,添加您在创建注释时分配给每个注释的另一个属性(例如 int 或字符串),该属性不会更改,以便您稍后可以使用该值找到注释。

例如,假设您将一个名为 annotationId 的 int 属性添加到您的注释类中,并且您想要使用 id# 42 更新注释:

BOOL annFound = NO;

//loop through the map view's annotations array to find annotation id# 42...
for (id<MKAnnotation> ann in mapView.annotations)
{
    if ([ann isKindOfClass:[MyAnnotationClass class]])
    {
        MyAnnotationClass *myAnn = (MyAnnotationClass *)ann;
        if (myAnn.annotationId == 42)
        {
            annFound = YES;
            myAnn.title = @"some new title";
            break;
        }
    }
}

if (!annFound)
{
    //annotation id# 42 is not yet on the map.
    //create it and add to map... 
}

关于ios - 在 map View 上重新添加注释以更新其标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12415778/

有关ios - 在 map View 上重新添加注释以更新其标题的更多相关文章

  1. ruby - 我需要将 Bundler 本身添加到 Gemfile 中吗? - 2

    当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/

  2. ruby-on-rails - 如何验证 update_all 是否实际在 Rails 中更新 - 2

    给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru

  3. ruby - 将 Bootstrap Less 添加到 Sinatra - 2

    我有一个ModularSinatra应用程序,我正在尝试将Bootstrap添加到应用程序中。get'/bootstrap/application.css'doless:"bootstrap/bootstrap"end我在views/bootstrap中有所有less文件,包括bootstrap.less。我收到这个错误:Less::ParseErrorat/bootstrap/application.css'reset.less'wasn'tfound.Bootstrap.less的第一行是://CSSReset@import"reset.less";我尝试了所有不同的路径格式,但它

  4. ruby - 如何在续集中重新加载表模式? - 2

    鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende

  5. ruby - 续集在添加关联时访问many_to_many连接表 - 2

    我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以

  6. ruby-on-rails - active_admin 目录中的常量警告重新声明 - 2

    我正在使用active_admin,我在Rails3应用程序的应用程序中有一个目录管理,其中包含模型和页面的声明。时不时地我也有一个类,当那个类有一个常量时,就像这样:classFooBAR="bar"end然后,我在每个必须在我的Rails应用程序中重新加载一些代码的请求中收到此警告:/Users/pupeno/helloworld/app/admin/billing.rb:12:warning:alreadyinitializedconstantBAR知道发生了什么以及如何避免这些警告吗? 最佳答案 在纯Ruby中:classA

  7. ruby-on-rails - 使用 rails 4 设计而不更新用户 - 2

    我将应用程序升级到Rails4,一切正常。我可以登录并转到我的编辑页面。也更新了观点。使用标准View时,用户会更新。但是当我添加例如字段:name时,它​​不会在表单中更新。使用devise3.1.1和gem'protected_attributes'我需要在设备或数据库上运行某种更新命令吗?我也搜索过这个地方,找到了许多不同的解决方案,但没有一个会更新我的用户字段。我没有添加任何自定义字段。 最佳答案 如果您想允许额外的参数,您可以在ApplicationController中使用beforefilter,因为Rails4将参数

  8. ruby-on-rails - 使用 Rmagick 或 ImageMagick 在背景上放置标题 - 2

    我有一张背景图片,我想在其中添加一个文本框。我想弄清楚如何将标题放置在其顶部的正确位置。(我使用标题是因为我需要自动换行功能)。现在,我只能让文本显示在左上角,但我需要能够手动定位它的开始位置。require'RMagick'require'Pry'includeMagicktext="Loremipsumdolorsitamet"img=ImageList.new('template001.jpg')img 最佳答案 这是使用convert的ImageMagick命令行的答案。如果你想在Rmagick中使用这个方法,你必须自己移植

  9. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  10. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

随机推荐