Repository navigation
Expand file tree
/
Copy pathCDAMapViewController.m
More file actions
154 lines (114 loc) · 4.83 KB
/
Copy pathCDAMapViewController.m
File metadata and controls
154 lines (114 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//
// CDAMapViewController.m
// ContentfulSDK
//
// Created by Boris Bügling on 02/04/14.
//
//
@import MapKit;
#import <ContentfulDeliveryAPI/CDAMapViewController.h>
#import <ContentfulDeliveryAPI/ContentfulDeliveryAPI.h>
#import "CDAUtilities.h"
@interface CDAMapAnnotation : NSObject <MKAnnotation>
@property (nonatomic) CLLocationCoordinate2D coordinate;
@property (nonatomic) NSString* identifier;
@property (nonatomic, copy) NSString* subtitle;
@property (nonatomic, copy) NSString* title;
@end
#pragma mark -
@implementation CDAMapAnnotation
@synthesize coordinate = _coordinate;
@synthesize identifier = _identifier;
@synthesize subtitle = _subtitle;
@synthesize title = _title;
@end
#pragma mark -
@interface CDAMapViewController ()
@property (nonatomic, readonly) NSString* cacheFileName;
@property (nonatomic) CDAArray* entries;
@property (nonatomic) MKMapView* mapView;
@end
#pragma mark -
@implementation CDAMapViewController
-(void)adjustMapRectToAnnotations {
MKMapRect zoomRect = MKMapRectNull;
for (id <MKAnnotation> annotation in self.mapView.annotations) {
MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.1, 0.1);
zoomRect = MKMapRectUnion(zoomRect, pointRect);
}
[self.mapView setVisibleMapRect:zoomRect animated:YES];
}
-(NSString *)cacheFileName {
return CDACacheFileNameForQuery(self.client, CDAResourceTypeEntry, self.query);
}
-(void)handleCaching {
if (self.offlineCaching) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self.entries writeToFile:self.cacheFileName];
});
}
}
-(NSArray *)items {
return self.entries.items;
}
-(void)refresh {
[self.mapView removeAnnotations:self.mapView.annotations];
for (CDAEntry* entry in self.entries.items) {
CDAMapAnnotation* annotation = [CDAMapAnnotation new];
annotation.identifier = entry.identifier;
if (self.coordinateFieldIdentifier) {
NSString* identifier = self.coordinateFieldIdentifier;
annotation.coordinate = [entry CLLocationCoordinate2DFromFieldWithIdentifier:identifier];
}
if (self.subtitleFieldIdentifier) {
NSString* subtitleFieldIdentifier = self.subtitleFieldIdentifier;
annotation.subtitle = entry.fields[subtitleFieldIdentifier];
}
if (self.titleFieldIdentifier) {
NSString* titleFieldIdentifier = self.titleFieldIdentifier;
annotation.title = entry.fields[titleFieldIdentifier];
}
[self.mapView addAnnotation:annotation];
}
[self adjustMapRectToAnnotations];
}
-(void)showError:(NSError*)error {
#if !defined(CF_APP_EXTENSIONS)
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error", nil)
message:error.localizedDescription
delegate:nil
cancelButtonTitle:NSLocalizedString(@"OK", nil)
otherButtonTitles:nil];
[alertView show];
#endif
}
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSAssert(self.client, @"You need to supply a client instance to %@.",
NSStringFromClass([self class]));
[self.client fetchEntriesMatching:self.query
success:^(CDAResponse *response, CDAArray *array) {
self.entries = array;
[self refresh];
[self handleCaching];
}
failure:^(CDAResponse *response, NSError *error) {
CDAClient* client = self.client;
NSParameterAssert(client);
if (CDAIsNoNetworkError(error) && client) {
self.entries = [CDAArray readFromFile:self.cacheFileName
client:client];
[self refresh];
return;
}
[self showError:error];
}];
}
-(void)viewDidLoad {
[super viewDidLoad];
self.mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
self.mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:self.mapView];
}
@end