【问题标题】:Searching for particular MKAnnotation class in MKMapView在 MKMapView 中搜索特定的 MKAnnotation 类
【发布时间】:2012-01-07 06:01:14
【问题描述】:

我在mapView中有4-5种不同的注解类。 使用以下代码,我希望只有 AnnotationType1 应该响应 for 循环。

for (AnnotationType1* annotation in mymap.annotations) 
        {
NSLog(@"annotation class is %@", [annotation class]);
}

但从控制台可以看出,我也有其他课程。

annotation class is AnnotationType1
annotation class is AnnotationType2
annotation class is AnnotationType3
annotation class is AnnotationType4

仅对说 AnnotationType1 注释执行操作的最佳方式是什么?

【问题讨论】:

    标签: iphone ios filter mkmapview mkannotation


    【解决方案1】:

    首先,正如您所发现的,快速迭代并不像您想象的那样工作。 mymap.annotations 无论如何都会返回相同的注释对象数组——它不知道你将它们分配给什么样的指针。

    其次,依靠视图(例如 MKMapView)来存储数据(例如您的注释)通常被认为是个坏主意。地图视图了解注释很好——它必须了解它们才能正确完成工作。但我不建议指望地图视图来维护应用程序的状态。您可能将注释对象存储在数据模型中的某个位置——如果是这样,那将是获取注释列表的更好位置。

    第三,您可以使用谓词过滤数组。 See this answer 帮助使用谓词按类名过滤。

    【讨论】:

    • 非常感谢@Caleb。那非常有用。我将有单独的数组来存储不同类型的注释类型。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多