jjzjj

objective-c - MKAnnotation 删除(处理器重)

coder 2024-01-12 原文

此函数接受纬度/经度对数组。它将所有这些转换为 MKAnnotation,然后对于 map 上当前存在的每个注释,它检查它是否存在于新的注释集中。如果它存在,它将按原样保留注释,否则将其删除。

然后对于每个新注释,它检查它当前是否在 map 上;如果是,则保留它,否则将其删除。

这显然非常密集,我想知道是否有更快的方法?

- (void)setAnnotationWithArray:(NSArray *)array {
    static BOOL processing = NO;
    if (processing) {
        return;
    }
    dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        processing = YES;


        NSMutableArray *annotationsArray = [NSMutableArray arrayWithCapacity:[array count]];
        NSMutableArray *annotationsToRemove = [NSMutableArray array];

        for (NSDictionary *dict in array) {
            NSString *latStr = [dict objectForKey:@"Latitude"];
            NSString *lonStr = [dict objectForKey:@"Longitude"];
            NSString *title = [dict objectForKey:@"Location"];

            double lat = [latStr doubleValue];
            double lon = [lonStr doubleValue];


            CLLocationCoordinate2D location;
            location.latitude = lat;
            location.longitude = lon;

            MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc] initWithTitle:title andCoordinate:location];
            [annotationsArray addObject:newAnnotation];
            [newAnnotation release];
        }

        for (id<MKAnnotation> oldAnnotation in [mv annotations]) {
            CLLocationCoordinate2D oldCoordinate = [oldAnnotation coordinate];

            BOOL exists = NO;
            for (MapViewAnnotation *newAnnontation in annotationsArray) {
                CLLocationCoordinate2D newCoordinate = [newAnnontation coordinate];
                if ((newCoordinate.latitude == oldCoordinate.latitude)
                    && (newCoordinate.longitude == oldCoordinate.longitude)) {
                    exists = YES;
                    break;
                }
            }

            if (!exists) {
                [annotationsToRemove addObject:oldAnnotation];
            }
        }


        [annotationsArray removeObjectsInArray:[mv annotations]];
        dispatch_async( dispatch_get_main_queue(), ^{
            processing = NO;
            [mv removeAnnotations:annotationsToRemove];
            [mv addAnnotations:annotationsArray];
        });
    });



}

最佳答案

你可以使用 removeObjectsInArray:(NSArray *)。

例如:

NSMutableArray *annotationsToRemove = [[NSMutableArray alloc] initWithArray:[mapView annotations]];
[annotationsToRemove removeObjectsInArray:annotationsArray];
NSMutableArray *annotationsToAdd = [[NSMutableArray alloc] initWithArray:annotationsArray];
[annotationsToAdd removeObjectsInArray:[mapView annotations]];

它假设您的注解实现了 hash 和 isEqual: 但应该更有效。

关于objective-c - MKAnnotation 删除(处理器重),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7121123/

有关objective-c - MKAnnotation 删除(处理器重)的更多相关文章

  1. ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法? - 2

    类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc

  2. ruby - 如何指定 Rack 处理程序 - 2

    Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack

  3. ruby-on-rails - 如何从 format.xml 中删除 <hash></hash> - 2

    我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为

  4. ruby - 我可以使用 Ruby 从 CSV 中删除列吗? - 2

    查看Ruby的CSV库的文档,我非常确定这是可能且简单的。我只需要使用Ruby删除CSV文件的前三列,但我没有成功运行它。 最佳答案 csv_table=CSV.read(file_path_in,:headers=>true)csv_table.delete("header_name")csv_table.to_csv#=>ThenewCSVinstringformat检查CSV::Table文档:http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV/Table.html

  5. 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

  6. ruby - 我可以使用 aws-sdk-ruby 在 AWS S3 上使用事务性文件删除/上传吗? - 2

    我发现ActiveRecord::Base.transaction在复杂方法中非常有效。我想知道是否可以在如下事务中从AWSS3上传/删除文件:S3Object.transactiondo#writeintofiles#raiseanexceptionend引发异常后,每个操作都应在S3上回滚。S3Object这可能吗?? 最佳答案 虽然S3API具有批量删除功能,但它不支持事务,因为每个删除操作都可以独立于其他操作成功/失败。该API不提供任何批量上传功能(通过PUT或POST),因此每个上传操作都是通过一个独立的API调用完成的

  7. ruby-on-rails - 如果 Object::try 被发送到一个 nil 对象,为什么它会起作用? - 2

    如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象

  8. ruby - 如何安全地删除文件? - 2

    在Ruby中是否有Gem或安全删除文件的方法?我想避免系统上可能不存在的外部程序。“安全删除”指的是覆盖文件内容。 最佳答案 如果您使用的是*nix,一个很好的方法是使用exec/open3/open4调用shred:`shred-fxuz#{filename}`http://www.gnu.org/s/coreutils/manual/html_node/shred-invocation.html检查这个类似的帖子:Writingafileshredderinpythonorruby?

  9. ruby-on-rails - 标准化文件名的字符串,删除重音和特殊字符 - 2

    我正在尝试找到一种方法来规范化字符串以将其作为文件名传递。到目前为止我有这个:my_string.mb_chars.normalize(:kd).gsub(/[^\x00-\x7F]/n,'').downcase.gsub(/[^a-z]/,'_')但第一个问题:-字符。我猜这个方法还有更多问题。我不控制名称,名称字符串可以有重音符、空格和特殊字符。我想删除所有这些,用相应的字母('é'=>'e')替换重音符号,并将其余的替换为'_'字符。名字是这样的:“Prélèvements-常规”“健康证”...我希望它们像一个没有空格/特殊字符的文件名:“prelevements_routin

  10. objective-c - 在设置 Cocoa Pods 和安装 Ruby 更新时出错 - 2

    我正在尝试为我的iOS应用程序设置cocoapods但是当我执行命令时:sudogemupdate--system我收到错误消息:当前已安装最新版本。中止。当我进入cocoapods的下一步时:sudogeminstallcocoapods我在MacOS10.8.5上遇到错误:ERROR:Errorinstallingcocoapods:cocoapods-trunkrequiresRubyversion>=2.0.0.我在MacOS10.9.4上尝试了同样的操作,但出现错误:ERROR:Couldnotfindavalidgem'cocoapods'(>=0),hereiswhy:U

随机推荐