This repository has been archived by the owner on Jul 22, 2020. It is now read-only.
/
HNKDemoViewController.m
242 lines (187 loc) · 8.48 KB
/
HNKDemoViewController.m
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//
// HNKDemoViewController.m
// HNKGooglePlacesAutocomplete-Example
//
// Created by Harlan Kellaway on 4/26/15.
// Copyright (c) 2015 Harlan Kellaway. All rights reserved.
//
#import "HNKDemoViewController.h"
#import <CoreLocation/CLPlacemark.h>
#import <HNKGooglePlacesAutocomplete/HNKGooglePlacesAutocomplete.h>
#import <MapKit/MapKit.h>
#import "CLPlacemark+HNKAdditions.h"
static NSString *const kHNKDemoMapAnnotationIdentifier = @"HNKDemoMapAnnotationIdentifier";
static NSString *const kHNKDemoSearchResultsCellIdentifier = @"HNKDemoSearchResultsCellIdentifier";
@interface HNKDemoViewController () <MKMapViewDelegate, UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) MKPointAnnotation *selectedPlaceAnnotation;
@property (nonatomic, strong) NSArray *searchResults;
@property (nonatomic, strong) HNKGooglePlacesAutocompleteQuery *searchQuery;
@property (nonatomic, assign) BOOL shouldBeginEditing;
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@end
@implementation HNKDemoViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.searchQuery = [HNKGooglePlacesAutocompleteQuery sharedQuery];
self.shouldBeginEditing = YES;
}
#pragma mark - Protocol Conformance
#pragma mark MKMapView Delegate
- (MKAnnotationView *)mapView:(MKMapView *)mapViewIn viewForAnnotation:(id<MKAnnotation>)annotation
{
if (mapViewIn != self.mapView || [annotation isKindOfClass:[MKUserLocation class]]) {
return nil;
}
MKPinAnnotationView *annotationView = (MKPinAnnotationView *)
[self.mapView dequeueReusableAnnotationViewWithIdentifier:kHNKDemoMapAnnotationIdentifier];
if (!annotationView) {
annotationView =
[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:kHNKDemoMapAnnotationIdentifier];
}
annotationView.animatesDrop = YES;
annotationView.canShowCallout = YES;
UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[detailButton addTarget:self
action:@selector(annotationDetailButtonPressed:)
forControlEvents:UIControlEventTouchUpInside];
annotationView.rightCalloutAccessoryView = detailButton;
return annotationView;
}
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
[self.mapView selectAnnotation:self.selectedPlaceAnnotation animated:YES];
}
- (void)annotationDetailButtonPressed:(id)sender
{
NSLog(@"Pin tapped");
}
#pragma mark UISearchDisplayDelegate
- (void)handleSearchForSearchString:(NSString *)searchString
{
if ([searchString isEqualToString:@""]) {
[self clearSearchResults];
} else {
[self.searchQuery fetchPlacesForSearchQuery:searchString
completion:^(NSArray *places, NSError *error) {
if (error) {
UIAlertView *alert =
[[UIAlertView alloc] initWithTitle:@"Could not fetch Places"
message:error.localizedDescription
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
[alert show];
} else {
self.searchResults = places;
[self.searchDisplayController.searchResultsTableView reloadData];
}
}];
}
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString
{
[self handleSearchForSearchString:searchString];
return YES;
}
#pragma mark -
#pragma mark UISearchBar Delegate
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if (![searchBar isFirstResponder]) {
self.shouldBeginEditing = NO;
[self.searchDisplayController setActive:NO];
[self.mapView removeAnnotation:self.selectedPlaceAnnotation];
}
}
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
if (self.shouldBeginEditing) {
// Animate in the table view.
NSTimeInterval animationDuration = 0.3;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:animationDuration];
self.searchDisplayController.searchResultsTableView.alpha = 0.75;
[UIView commitAnimations];
[self.searchDisplayController.searchBar setShowsCancelButton:YES animated:YES];
}
BOOL boolToReturn = self.shouldBeginEditing;
self.shouldBeginEditing = YES;
return boolToReturn;
}
#pragma mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.searchResults count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kHNKDemoSearchResultsCellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:kHNKDemoSearchResultsCellIdentifier];
}
cell.textLabel.font = [UIFont fontWithName:@"GillSans" size:16.0];
cell.textLabel.text = [self placeAtIndexPath:indexPath].name;
return cell;
}
#pragma mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
HNKGooglePlacesAutocompletePlace *place = [self placeAtIndexPath:indexPath];
[CLPlacemark hnk_placemarkFromGooglePlace:place
apiKey:self.searchQuery.apiKey
completion:^(CLPlacemark *placemark, NSString *addressString, NSError *error) {
if (error) {
UIAlertView *alert =
[[UIAlertView alloc] initWithTitle:@"Could not map selected Place"
message:error.localizedDescription
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
[alert show];
} else if (placemark) {
[self addPlacemarkAnnotationToMap:placemark addressString:addressString];
[self recenterMapToPlacemark:placemark];
[self.searchDisplayController setActive:NO];
[self.searchDisplayController.searchResultsTableView
deselectRowAtIndexPath:indexPath
animated:NO];
}
}];
}
#pragma mark - Helpers
- (HNKGooglePlacesAutocompletePlace *)placeAtIndexPath:(NSIndexPath *)indexPath
{
return self.searchResults[indexPath.row];
}
#pragma mark Map Helpers
- (void)addPlacemarkAnnotationToMap:(CLPlacemark *)placemark addressString:(NSString *)address
{
[self.mapView removeAnnotation:self.selectedPlaceAnnotation];
self.selectedPlaceAnnotation = [[MKPointAnnotation alloc] init];
self.selectedPlaceAnnotation.coordinate = placemark.location.coordinate;
self.selectedPlaceAnnotation.title = address;
[self.mapView addAnnotation:self.selectedPlaceAnnotation];
}
- (void)recenterMapToPlacemark:(CLPlacemark *)placemark
{
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta = 0.02;
span.longitudeDelta = 0.02;
region.span = span;
region.center = placemark.location.coordinate;
[self.mapView setRegion:region];
}
#pragma mark Search Helpers
- (void)clearSearchResults
{
self.searchResults = @[];
}
- (void)handleSearchError:(NSError *)error
{
NSLog(@"ERROR = %@", error);
}
@end