简体   繁体   中英

Searching for particular MKAnnotation class in MKMapView

I have 4-5 kinds of different annotations classes in mapView. With following code I expect only AnnotationType1 should respond to for loop.

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

But as is evident from console I get other classes also.

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

what will be the best way to perform actions only on say AnnotationType1 annotation?

First, as you've discovered, fast iteration doesn't work the way you thought it did. mymap.annotations returns the same array of annotation objects no matter what -- it doesn't have any idea what kind of pointer you're assigning them to.

Second, it's usually considered a bad idea to count on a view (such as MKMapView) to store data (like your annotations). It's fine for the map view to know about the annotations -- it must know about them to do its job properly. But I wouldn't recommend counting on the map view to maintain the app's state. You probably have the annotation objects stored somewhere in your data model -- if so, that'd be a better place to get the list of annotations.

Third, you can filter the array using a predicate. See this answer for help using a predicate to filter by class name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM