Skip to content

Commit d609a7e

Browse files
committed
adds more tests, comments, extensions
1 parent 0e275be commit d609a7e

17 files changed

+453
-51
lines changed

EPMapExtensions/EPAnnotationArray.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,53 @@
1717
NSMutableArray *_annotationArray;
1818
}
1919

20+
/**
21+
Annotation Collection class which wraps up a mutable a `NSMutableArray` and contains `MKAnnotation` protocol conform objects
22+
*/
23+
2024
- (id)initWithAnnotationArray:(NSArray *)annotations;
2125

26+
/**
27+
Annotation Collection class which wraps up a mutable a `NSMutableArray` and contains `MKAnnotation` protocol conform objects
28+
*/
29+
2230
- (NSArray *)allAnnotations;
2331

32+
/**
33+
Annotation Collection class which wraps up a mutable a `NSMutableArray` and contains `MKAnnotation` protocol conform objects
34+
*/
35+
2436
- (NSArray *)annotationsConformsToProtocol:(Protocol *)protocol;
37+
38+
/**
39+
Annotation Collection class which wraps up a mutable a `NSMutableArray` and contains `MKAnnotation` protocol conform objects
40+
*/
41+
2542
- (NSArray *)annotationsConformsToProtocols:(NSArray *)protocols;
2643

44+
/**
45+
Annotation Collection class which wraps up a mutable a `NSMutableArray` and contains `MKAnnotation` protocol conform objects
46+
*/
47+
2748
- (NSArray *)annotationsOfKindOfClass:(Class<MKAnnotation>)cls;
49+
50+
/**
51+
Annotation Collection class which wraps up a mutable a `NSMutableArray` and contains `MKAnnotation` protocol conform objects
52+
*/
53+
2854
- (NSArray *)annotationsOfKindOfClasses:(NSArray *)classes;
2955

56+
/**
57+
Annotation Collection class which wraps up a mutable a `NSMutableArray` and contains `MKAnnotation` protocol conform objects
58+
*/
59+
3060
- (NSArray *)annotationsWithinRange:(CLLocationDistance)radius center:(CLLocationCoordinate2D)center;
3161

62+
/**
63+
Annotation Collection class which wraps up a mutable a `NSMutableArray` and contains `MKAnnotation` protocol conform objects
64+
*/
65+
66+
3267
- (NSUInteger)count;
3368

3469
@end

EPMapExtensions/EPAnnotationArray.m

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
@implementation EPAnnotationArray
1212

13+
#pragma mark - Obj Alloc/Dealloc
14+
1315
- (id)init
1416
{
1517
if (self = [super init]) {
@@ -26,6 +28,9 @@ - (id)initWithAnnotationArray:(NSArray *)annotations
2628
return self;
2729
}
2830

31+
#pragma mark - Collection Mutators
32+
33+
2934
- (void)addAnnotations:(NSArray *)annotations
3035
{
3136
for (id annotation in annotations)
@@ -69,6 +74,39 @@ - (void)removeAnnotationsConformsToProtocols:(NSArray *)protocols
6974
}
7075
}
7176

77+
- (void)removeAnnotationsOfKindOfClass:(Class<MKAnnotation>)cls
78+
{
79+
NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
80+
return ![evaluatedObject isKindOfClass:cls];
81+
}];
82+
[_annotationArray filterUsingPredicate:predicate];
83+
}
84+
85+
- (void)removeAnnotationsOfKindOfClasses:(NSArray *)classes
86+
{
87+
for (Class<MKAnnotation> cls in classes) {
88+
[self removeAnnotationsOfKindOfClass:cls];
89+
}
90+
}
91+
92+
- (void)removeAnnotation:(id<MKAnnotation>)annotation
93+
{
94+
if ( ! [annotation conformsToProtocol:@protocol(MKAnnotation)] )
95+
[NSException raise:NSInternalInconsistencyException format:@"annotation should respond to MKAnnotation protocol"];
96+
[_annotationArray removeObject:annotation];
97+
}
98+
99+
- (void)removeAnnotations:(NSArray *)annotations
100+
{
101+
for (id annotation in annotations)
102+
if (! [annotation conformsToProtocol:@protocol(MKAnnotation)])
103+
[NSException raise:NSInternalInconsistencyException format:@"annotations should contains only objs conforms to MKAnnotation protocol"];
104+
[_annotationArray removeObjectsInArray:annotations];
105+
}
106+
107+
#pragma mark - Collection Filters/Helpers
108+
109+
72110
- (NSArray *)annotationsConformsToProtocol:(Protocol *)protocol
73111
{
74112
NSPredicate *predicate = [NSPredicate predicateWithBlock: ^BOOL(id evaluatedObject, NSDictionary *bindings ) {
@@ -103,21 +141,6 @@ - (NSArray *)annotationsOfKindOfClass:(Class<MKAnnotation> )cls
103141
return [_annotationArray filteredArrayUsingPredicate:predicate];
104142
}
105143

106-
- (void)removeAnnotationsOfKindOfClass:(Class<MKAnnotation>)cls
107-
{
108-
NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
109-
return ![evaluatedObject isKindOfClass:cls];
110-
}];
111-
[_annotationArray filterUsingPredicate:predicate];
112-
}
113-
114-
- (void)removeAnnotationsOfKindOfClasses:(NSArray *)classes
115-
{
116-
for (Class<MKAnnotation> cls in classes) {
117-
[self removeAnnotationsOfKindOfClass:cls];
118-
}
119-
}
120-
121144
- (NSArray *)annotationsWithinRange:(CLLocationDistance)radius center:(CLLocationCoordinate2D)center
122145
{
123146
CLLocation *centerPosition = [[CLLocation alloc] initWithLatitude:center.latitude longitude:center.longitude];
@@ -129,29 +152,17 @@ - (NSArray *)annotationsWithinRange:(CLLocationDistance)radius center:(CLLocatio
129152
return [_annotationArray filteredArrayUsingPredicate:predicate];
130153
}
131154

132-
- (void)removeAnnotation:(id<MKAnnotation>)annotation
133-
{
134-
if ( ! [annotation conformsToProtocol:@protocol(MKAnnotation)] )
135-
[NSException raise:NSInternalInconsistencyException format:@"annotation should respond to MKAnnotation protocol"];
136-
[_annotationArray removeObject:annotation];
137-
}
138-
139-
- (void)removeAnnotations:(NSArray *)annotations
140-
{
141-
for (id annotation in annotations)
142-
if (! [annotation conformsToProtocol:@protocol(MKAnnotation)])
143-
[NSException raise:NSInternalInconsistencyException format:@"annotations should contains only objs conforms to MKAnnotation protocol"];
144-
[_annotationArray removeObjectsInArray:annotations];
145-
}
146-
147155
- (NSUInteger)count
148156
{
149157
return [_annotationArray count];
150158
}
151159

160+
#pragma mark - Overridden Methods
161+
162+
152163
- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(__unsafe_unretained id [])buffer count:(NSUInteger)len
153164
{
154165
return [_annotationArray countByEnumeratingWithState:state objects:buffer count:len];
155166
}
156167

157-
@end
168+
@end

EPMapExtensions/EPMapView.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,18 @@
5050

5151
- (MKCoordinateRegion)centeredRegionForAnnotationConformingToProtocols:(NSArray *)protocols;
5252

53+
/**
54+
Computes the centered region with respect to all annotations belonging to a given kind of class
55+
@param cls The kind of class used to filter annotations
56+
*/
57+
5358
- (MKCoordinateRegion)centeredRegionForAnnotationOfKindOfClass:(Class)cls;
5459

60+
/**
61+
Computes the centered region with respect to all annotations belonging to a given pool of classes
62+
@param classes Array of classes used to filter the annotations
63+
*/
64+
5565
- (MKCoordinateRegion)centeredRegionForAnnotationOfKindOfClasses:(NSArray *)classes;
5666

5767
///---------------------------------------------
@@ -78,10 +88,19 @@
7888

7989
- (NSUInteger)visibleAnnotationsConformingToProtocols:(NSArray *)protocols;
8090

91+
/**
92+
Computes the number of visible annotations displayed over the visible region of the map conforming to a given type of class
93+
@param cls The class used to filter annotations
94+
*/
8195

8296
- (NSUInteger)visibleAnnotationsOfKindOfClass:(Class)cls;
8397

84-
- (NSUInteger)visibleAnnotationOfKindOfClasses:(NSArray *)classes;
98+
/**
99+
Computes the number of visible annotations displayed over the visible region of the map conforming to a given pool of classes
100+
@param classes The array of classes to filter annotations
101+
*/
102+
103+
- (NSUInteger)visibleAnnotationsOfKindOfClasses:(NSArray *)classes;
85104

86105

87106
///---------------------------------------------

EPMapExtensions/EPMapView.m

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,27 @@ @implementation EPMapView {
1717
EPAnnotationArray *_annotationArray;
1818
}
1919

20+
#pragma mark - Obj alloc/init/dealloc
2021

2122
- (id)initWithMapView:(MKMapView *)mapView
2223
{
2324
if (self = [super initWithFrame:mapView.frame]) {
2425
_annotationArray = [[EPAnnotationArray alloc] init];
26+
[_annotationArray addAnnotations:[mapView annotations]];
2527
}
2628
return self;
2729
}
2830

31+
- (id)initWithFrame:(CGRect)frame
32+
{
33+
if (self = [super initWithFrame:frame]) {
34+
_annotationArray = [[EPAnnotationArray alloc] init];
35+
}
36+
return self;
37+
}
38+
39+
#pragma mark - Collection mutators
40+
2941
- (void)addAnnotations:(NSArray *)annotations
3042
{
3143
[super addAnnotations:annotations];
@@ -56,6 +68,9 @@ - (void)removeAllAnnotations
5668
[_annotationArray removeAllAnnotations];
5769
}
5870

71+
72+
#pragma mark - Calculations of centered regions
73+
5974
- (MKCoordinateRegion)centeredRegionForAnnotations:(NSArray *)annotationArray
6075
{
6176
if (![annotationArray count]) {
@@ -89,8 +104,8 @@ - (MKCoordinateRegion)centeredRegionForAnnotations:(NSArray *)annotationArray
89104

90105
}
91106
CLLocationCoordinate2D baricentrum = CLLocationCoordinate2DMake((sum_y/ncoord), (sum_x/ncoord));
92-
latDelta = fabs(max_lat - min_lat) * (1 + .08) ;
93-
lonDelta = fabs(max_lon - min_lon) * (1 + .08) ;
107+
latDelta = fabs(max_lat - min_lat);
108+
lonDelta = fabs(max_lon - min_lon);
94109
MKCoordinateSpan span = MKCoordinateSpanMake(latDelta, lonDelta);
95110
return [self regionThatFits:MKCoordinateRegionMake(baricentrum, span)];
96111

@@ -111,6 +126,19 @@ - (MKCoordinateRegion)centeredRegionForAnnotationConformingToProtocols:(NSArray
111126
return [self centeredRegionForAnnotations:[_annotationArray annotationsConformsToProtocols:protocols]];
112127
}
113128

129+
- (MKCoordinateRegion)centeredRegionForAnnotationOfKindOfClass:(Class)cls
130+
{
131+
return [self centeredRegionForAnnotations:[_annotationArray annotationsOfKindOfClass:cls]];
132+
}
133+
134+
- (MKCoordinateRegion)centeredRegionForAnnotationOfKindOfClasses:(NSArray *)classes
135+
{
136+
return [self centeredRegionForAnnotationOfKindOfClasses:[_annotationArray annotationsOfKindOfClasses:classes]];
137+
}
138+
139+
140+
#pragma mark - Visible Annotations
141+
114142
- (NSUInteger)visibleAnnotations
115143
{
116144
return [[self annotationsInMapRect:[self visibleMapRect]] count];
@@ -133,4 +161,21 @@ - (NSUInteger)visibleAnnotationsConformingToProtocols:(NSArray *)protocols
133161
return counter;
134162
}
135163

164+
- (NSUInteger)visibleAnnotationsOfKindOfClass:(Class)cls
165+
{
166+
NSPredicate* p = [NSPredicate predicateWithBlock: ^BOOL(id evaluatedObject, NSDictionary *bindings) {
167+
return ([evaluatedObject isKindOfClass:cls]);
168+
}];
169+
return [[[self annotationsInMapRect:[self visibleMapRect]] filteredSetUsingPredicate:p] count];
170+
}
171+
172+
- (NSUInteger)visibleAnnotationsOfKindOfClasses:(NSArray *)classes
173+
{
174+
NSUInteger counter = 0;
175+
for (Class cls in classes) {
176+
counter += [self visibleAnnotationsOfKindOfClass:cls];
177+
}
178+
return counter;
179+
}
180+
136181
@end

EPMapExtensions/EPMapViewDecorator.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313

1414
/**
15-
`EPMapViewDecoratorProtocol` is a subset of `MKMapViewDelegate`.
15+
`EPMapViewDecoratorProtocol` encompasses a subset of `MKMapViewDelegate` MKMapKit protocol.
1616
*/
1717

1818
@protocol EPMapViewDecoratorProtocol <NSObject>
@@ -98,7 +98,7 @@
9898
Registers annotation view associated to any annotation point class responding to `MKAnnotation` protocol.
9999
100100
@param cls Any class responding to `MKAnnotation` protocol.
101-
@param annotationToAnnotationView A block that given an annotation returns the name of annotaitionView class associated with it.
101+
@param annotationToAnnotationView A block that given an annotation returns the name of annotationView class associated with it.
102102
103103
@exception NSInvalidArgumentException It is thrown when the annotation param is not matching any `MKAnnotationView` class or its subclass
104104
*/
@@ -113,7 +113,7 @@
113113
/**
114114
Sets the mapping between the annotation and the correspondent annotation view.
115115
116-
@param translationBlock A block that given an annotation returns the name of annotaitionView class associated with it.
116+
@param translationBlock A block that given an annotation returns the name of annotationView class associated with it.
117117
118118
*/
119119

EPMapExtensions/EPMapViewDecorator.m

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ - (void)registerAnnotationViewForClass:(Class<MKAnnotation>)cls annotationView:(
3939
if ([NSClassFromString(annotation) isSubclassOfClass:[MKAnnotationView class]])
4040
_annotationViewForClass[NSStringFromClass(cls)] = NSClassFromString(annotation);
4141
else
42-
[[NSException exceptionWithName:NSInvalidArgumentException reason:@"Annotation should be any subclass of the MKAnnotationView"
42+
[[NSException exceptionWithName:NSInvalidArgumentException
43+
reason:[NSString stringWithFormat:@"Annotation %@ should be any subclass of the MKAnnotationView", annotation]
4344
userInfo:nil] raise];
4445
}
4546

@@ -86,11 +87,12 @@ - (void)registerAnnotationViewForClass:(Class <MKAnnotation>)cls translationBloc
8687

8788
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
8889
{
90+
91+
if ([annotation class] == [MKUserLocation class]) return _userLocationAnnotationView;
92+
8993
if ([self EP_isNoneRegistered])
9094
return _defaultAnnotationView;
9195

92-
if ([annotation class] == [MKUserLocation class]) return _userLocationAnnotationView;
93-
9496
MKAnnotationView* annotationView;
9597
NSString *reuseIdentifier = [self EP_reuseIdentifierForAnnotation:annotation];
9698

@@ -127,7 +129,8 @@ - (Class)EP_annotationViewClassFromAnnotation:(id<MKAnnotation>)annotation
127129
Class annotationViewClass = NSClassFromString([self EP_reuseIdentifierForAnnotation:annotation]);
128130

129131
if (! [annotationViewClass isSubclassOfClass:[MKAnnotationView class]] )
130-
[[NSException exceptionWithName:NSInvalidArgumentException reason:[NSString stringWithFormat:@"%@ should be any subclass of the MKAnnotationView", annotationViewClass]
132+
[[NSException exceptionWithName:NSInvalidArgumentException
133+
reason:[NSString stringWithFormat:@"%@ should be any subclass of the MKAnnotationView", annotationViewClass]
131134
userInfo:nil] raise];
132135

133136
return annotationViewClass;

EPMapExtensions/EPMapViewDelegate.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,14 @@
1111

1212
@protocol EPMapViewDecoratorProtocol;
1313

14+
@class EPUserPosition;
15+
1416
@interface EPMapViewDelegate : NSObject <MKMapViewDelegate>
1517

16-
@property (nonatomic, weak) id<EPMapViewDecoratorProtocol> mapViewDecorator;
18+
@property (nonatomic, strong) id<EPMapViewDecoratorProtocol> mapViewDecorator;
19+
20+
- (void)registerNavigationBlock:(void(^)(UIView *sender))block annotationView:(MKAnnotationView *)annotationView;
21+
22+
@property (nonatomic, copy) void(^positionUpdated)(EPUserPosition *userPosition);
1723

1824
@end

0 commit comments

Comments
 (0)