1+ //
2+ // EPAnnotationArray.m
3+ // iOSMapExtension
4+ //
5+ // Created by epacces on 5/6/13.
6+ // Copyright (c) 2013 it.reply. All rights reserved.
7+ //
8+
9+ #import " EPAnnotationArray.h"
10+
11+ @implementation EPAnnotationArray
12+
13+ - (id )init
14+ {
15+ if (self = [super init ]) {
16+ _annotationArray = [[NSMutableArray alloc ] initWithCapacity: 1 ];
17+ }
18+ return self;
19+ }
20+
21+ - (void )addAnnotations : (NSArray *)annotations
22+ {
23+ for (id annotation in annotations)
24+ if (! [annotation conformsToProtocol: @protocol (MKAnnotation)])
25+ [NSException raise: NSInternalInconsistencyException format: @" annotations should contains only objs conforms to MKAnnotation protocol" ];
26+
27+ [_annotationArray addObjectsFromArray: annotations];
28+ }
29+
30+ - (void )addAnnotation : (id <MKAnnotation>)annotation
31+ {
32+ if ( ! [annotation conformsToProtocol: @protocol (MKAnnotation)] )
33+ [NSException raise: NSInternalInconsistencyException format: @" annotation should respond to MKAnnotation protocol" ];
34+ else
35+ [_annotationArray addObject: annotation];
36+ }
37+
38+ - (NSArray *)allAnnotations
39+ {
40+ return [_annotationArray copy ];
41+ }
42+
43+ - (void )removeAllAnnotations
44+ {
45+ [_annotationArray removeAllObjects ];
46+ }
47+
48+ - (void )removeAnnotationsConformsToProtocol : (Protocol *)protocol
49+ {
50+ NSPredicate *predicate = [NSPredicate predicateWithBlock: ^BOOL (id evaluatedObject, NSDictionary *bindings ) {
51+ return [evaluatedObject conformsToProtocol: protocol];
52+ }];
53+ [_annotationArray filterUsingPredicate: predicate];
54+ }
55+
56+
57+ - (void )removeAnnotationsConformsToProtocols : (NSArray *)protocols
58+ {
59+ for (Protocol *p in protocols) {
60+ [self removeAnnotationsConformsToProtocol: p];
61+ }
62+ }
63+
64+ - (NSArray *)annotationsConformsToProtocol : (Protocol *)protocol
65+ {
66+ NSPredicate *predicate = [NSPredicate predicateWithBlock: ^BOOL (id evaluatedObject, NSDictionary *bindings ) {
67+ return ![evaluatedObject conformsToProtocol: protocol];
68+ }];
69+ return [_annotationArray filteredArrayUsingPredicate: predicate];
70+ }
71+
72+ - (NSArray *)annotationsConformsToProtocols : (NSArray *)protocols
73+ {
74+ NSMutableArray *filteredArray = [[NSMutableArray alloc ] init ];
75+ for (Protocol *p in protocols) {
76+ [filteredArray addObjectsFromArray: [self annotationsConformsToProtocol: p]];
77+ }
78+ return filteredArray;
79+ }
80+
81+ - (NSArray *)annotationsWithinRange : (CLLocationDistance)radius center : (CLLocationCoordinate2D)center
82+ {
83+ CLLocation *centerPosition = [[CLLocation alloc ] initWithLatitude: center.latitude longitude: center.longitude];
84+ NSPredicate *predicate = [NSPredicate predicateWithBlock: ^BOOL (id <MKAnnotation> evaluatedObject, NSDictionary *bindings ) {
85+ CLLocationCoordinate2D coordinate = [evaluatedObject coordinate ];
86+ CLLocation *annotationPosition = [[CLLocation alloc ] initWithLatitude: coordinate.latitude longitude: coordinate.longitude];
87+ return [ annotationPosition distanceFromLocation: centerPosition ] <= radius;
88+ }];
89+ return [_annotationArray filteredArrayUsingPredicate: predicate];
90+ }
91+
92+ - (void )removeAnnotation : (id <MKAnnotation>)annotation
93+ {
94+ [_annotationArray removeObject: annotation];
95+ }
96+
97+ - (void )removeAnnotations : (NSArray *)annotations
98+ {
99+ [_annotationArray removeObjectsInArray: annotations];
100+ }
101+
102+ - (NSUInteger )count
103+ {
104+ return [_annotationArray count ];
105+ }
106+
107+ - (NSUInteger )countByEnumeratingWithState : (NSFastEnumerationState *)state objects : (__unsafe_unretained id [])buffer count : (NSUInteger )len
108+ {
109+ return [_annotationArray countByEnumeratingWithState: state objects: buffer count: len];
110+ }
111+
112+ @end
0 commit comments