forked from scottrhoyt/CenterPinMapViewController
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCenterPinMapViewController.m
More file actions
371 lines (301 loc) · 12.6 KB
/
CenterPinMapViewController.m
File metadata and controls
371 lines (301 loc) · 12.6 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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
//
// SHMapViewController.m
// Map View Controller
//
// Created by Scott Hoyt on 3/6/14.
// Copyright (c) 2014 Wild Onion Labs. All rights reserved.
//
#import "CenterPinMapViewController.h"
@interface CenterPinMapViewController () <MKMapViewDelegate>
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@property (strong, nonatomic) MKPointAnnotation *centerAnnotaion;
@property (strong, nonatomic) MKPinAnnotationView *centerAnnotationView;
@property (strong, nonatomic) UIToolbar *toolbar;
@property (nonatomic) BOOL lastValidZoomState;
@property (nonatomic, strong) CLGeocoder *geocoder;
@property (nonatomic, readwrite) CLLocationCoordinate2D selectedCoordinate;
@end
@implementation CenterPinMapViewController
#pragma mark - Setters/Getters
- (MKPointAnnotation *)centerAnnotaion
{
if (!_centerAnnotaion) {
_centerAnnotaion = [[MKPointAnnotation alloc] init];
}
return _centerAnnotaion;
}
- (MKPinAnnotationView *)centerAnnotationView
{
if (!_centerAnnotationView) {
_centerAnnotationView = [[MKPinAnnotationView alloc] initWithAnnotation:self.centerAnnotaion
reuseIdentifier:@"centerAnnotationView"];
_centerAnnotationView.pinColor = MKPinAnnotationColorPurple;
}
return _centerAnnotationView;
}
//- (CLLocationCoordinate2D)selectedCoordinate
//{
// return self.mapView.centerCoordinate;
//}
- (void)setMapView:(MKMapView *)mapView
{
// Remove ourselves as delegate to old and add to new
if (_mapView) {
_mapView.delegate = nil;
}
_mapView = mapView;
if (_mapView) {
mapView.delegate = self;
}
}
#define DEFAULT_INITIAL_SIZE 5000000
- (NSUInteger)initialMapSize
{
if (!_initialMapSize) {
_initialMapSize = DEFAULT_INITIAL_SIZE;
}
return _initialMapSize;
}
#define DEFAULT_ZOOM_SIZE 1000
- (NSUInteger)zoomMapSize
{
if (!_zoomMapSize) {
_zoomMapSize = DEFAULT_ZOOM_SIZE;
}
return _zoomMapSize;
}
- (void)setDoesDisplayPointAccuracyIndicators:(BOOL)doesDisplayPointAccuracyIndicators
{
_doesDisplayPointAccuracyIndicators = doesDisplayPointAccuracyIndicators;
[self updatePointAccuracyIndicators];
}
-(void)setRequiredPointAccuracy:(CLLocationDistance)requiredPointAccuracy
{
_requiredPointAccuracy = requiredPointAccuracy;
[self updatePointAccuracyIndicators];
}
- (void)setZoomToUser:(BOOL)zoomToUser
{
if (zoomToUser) {
if (self.mapView.showsUserLocation && self.mapView.userLocation.location && !self.mapView.userLocation.updating) {
[self changeRegionToCoordinate:self.mapView.userLocation.location.coordinate withSize:self.zoomMapSize];
_zoomToUser = NO;
} else {
_zoomToUser = YES;
}
} else
{
_zoomToUser = NO;
}
}
- (UIToolbar *)toolbar
{
if (!_toolbar) {
_toolbar = [[UIToolbar alloc] init];
_toolbar.opaque = NO;
_toolbar.translucent = YES;
[_toolbar setBackgroundImage:[UIImage new]
forToolbarPosition:UIBarPositionAny
barMetrics:UIBarMetricsDefault];
[_toolbar setShadowImage:[UIImage new]
forToolbarPosition:UIToolbarPositionAny];
MKUserTrackingBarButtonItem *userTrackingButton = [[MKUserTrackingBarButtonItem alloc] initWithMapView:self.mapView];
[_toolbar setItems:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], userTrackingButton]];
}
return _toolbar;
}
- (void)setShowUserTrackingButton:(BOOL)showUserTrackingButton
{
if (_showUserTrackingButton != showUserTrackingButton) {
_showUserTrackingButton = showUserTrackingButton;
if (self.view.window) {
if (_showUserTrackingButton) {
[self initToolbarAndAdd];
} else {
[self.toolbar removeFromSuperview];
}
}
}
}
- (CLGeocoder *)geocoder
{
if (!_geocoder) {
_geocoder = [[CLGeocoder alloc] init];
}
return _geocoder;
}
- (CLLocationCoordinate2D)userCoordinate
{
return self.mapView.userLocation.coordinate;
}
#pragma mark - View Controller Lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
[self.mapView addSubview:self.centerAnnotationView];
}
// Default center of the map is the geographic center of the US
#define DEFAULT_CENTER_LATTITUDE 39.8282
#define DEFAULT_CENTER_LONGITUDE -98.5795
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.mapView setRegion:MKCoordinateRegionMakeWithDistance(CLLocationCoordinate2DMake(DEFAULT_CENTER_LATTITUDE, DEFAULT_CENTER_LONGITUDE),self.initialMapSize, self.initialMapSize)];
}
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self moveMapAnnotationToCoordinate:self.mapView.centerCoordinate];
if (self.showUserTrackingButton) {
[self initToolbarAndAdd];
}
}
#pragma mark - main methods
- (void)initToolbarAndAdd
{
self.toolbar.frame = CGRectMake(0, self.view.frame.size.height - 44, self.view.frame.size.width, 44);
[self.mapView addSubview:self.toolbar];
}
// These are the constants need to offset distance between the lower left corner of
// the annotaion view and the head of the pin
#define PIN_WIDTH_OFFSET 7.75
#define PIN_HEIGHT_OFFSET 5
- (void)moveMapAnnotationToCoordinate:(CLLocationCoordinate2D) coordinate
{
CGPoint mapViewPoint = [self.mapView convertCoordinate:coordinate toPointToView:self.mapView];
// Offset the view from to account for distance from the lower left corner to the pin head
CGFloat xoffset = CGRectGetMidX(self.centerAnnotationView.bounds) - PIN_WIDTH_OFFSET;
CGFloat yoffset = -CGRectGetMidY(self.centerAnnotationView.bounds) + PIN_HEIGHT_OFFSET;
self.centerAnnotationView.center = CGPointMake(mapViewPoint.x + xoffset,
mapViewPoint.y + yoffset);
}
- (void)changeRegionToCoordinate:(CLLocationCoordinate2D)coordinate withSize:(NSUInteger)size
{
MKCoordinateRegion newRegion = MKCoordinateRegionMakeWithDistance(coordinate, size, size);
[self.mapView setRegion:newRegion animated:YES];
}
- (CLLocationDistance)metersPerViewPoint
{
CGRect comparisonRect = CGRectMake(self.mapView.center.x,
self.mapView.center.y,
1,
1);
MKCoordinateRegion comparisonRegion = [self.mapView convertRect:comparisonRect toRegionFromView:self.mapView];
CLLocationCoordinate2D comparisonCoordinate1 = CLLocationCoordinate2DMake(comparisonRegion.center.latitude - comparisonRegion.span.latitudeDelta,
comparisonRegion.center.longitude - comparisonRegion.span.longitudeDelta);
CLLocationCoordinate2D comparisonCoordinate2 = CLLocationCoordinate2DMake(comparisonRegion.center.latitude + comparisonRegion.span.latitudeDelta,
comparisonRegion.center.longitude + comparisonRegion.span.longitudeDelta);
CLLocationDistance sizeInMeters = MKMetersBetweenMapPoints(MKMapPointForCoordinate(comparisonCoordinate1),
MKMapPointForCoordinate(comparisonCoordinate2));
return sizeInMeters;
}
- (BOOL)mapIsAtValidZoomScale
{
if (self.requiredPointAccuracy) {
return [self metersPerViewPoint] <= self.requiredPointAccuracy;
} else {
return YES;
}
}
#define INDICATOR_BORDER_WIDTH 5
- (void)updatePointAccuracyIndicators
{
if (self.doesDisplayPointAccuracyIndicators && self.requiredPointAccuracy > 0) {
if ([self mapIsAtValidZoomScale]) {
self.mapView.layer.borderColor = [UIColor greenColor].CGColor;
self.mapView.layer.borderWidth = INDICATOR_BORDER_WIDTH;
} else {
self.mapView.layer.borderColor = [UIColor redColor].CGColor;
self.mapView.layer.borderWidth = INDICATOR_BORDER_WIDTH;
}
}
else
{
self.mapView.layer.borderWidth = 0;
}
}
- (CLLocation *)locationForCoordinate:(CLLocationCoordinate2D)coordinate
{
CLLocation *location = [[CLLocation alloc] initWithCoordinate:coordinate
altitude:0
horizontalAccuracy:1
verticalAccuracy:1
timestamp:[NSDate date]];
return location;
}
- (void)reverseGeoCodeAfterTimer:(NSTimer *)timer
{
CLLocation *location = (CLLocation *)timer.userInfo;
// If we are still at the same location after the delay, reverse geocod
if ((location.coordinate.latitude == self.selectedCoordinate.latitude) &&
(location.coordinate.longitude == self.selectedCoordinate.longitude)) {
CLGeocodeCompletionHandler handler = ^(NSArray *placemark, NSError *error){
if (error) {
NSLog(@"Error in geocode request. Error message: %@", error);
}
else
{
// If there is atleast one placemark, set the selected placemark and call delegate
if (placemark && ([placemark count] > 0)) {
self.selectedPlacemark = placemark[0];
NSLog(@"%@", placemark);
NSLog(@"Reverse location name: %@", self.selectedPlacemark.name);
if ([self.delegate respondsToSelector:@selector(centerPinMapViewController:didResolvePlacemark:)]) {
// Notify delegate on main thread
dispatch_async(dispatch_get_main_queue(), ^{
[self.delegate centerPinMapViewController:self didResolvePlacemark:placemark[0]];
});
}
}
}
};
[self.geocoder reverseGeocodeLocation:location completionHandler:handler];
}
}
#pragma mark - MapView Delegate methods
#define REVERSE_GEOCODE_DELAY 2.0
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
BOOL currentZoomStateValid = [self mapIsAtValidZoomScale];
if (self.lastValidZoomState != currentZoomStateValid) {
self.lastValidZoomState = currentZoomStateValid;
if (self.doesDisplayPointAccuracyIndicators && self.requiredPointAccuracy > 0) {
[self updatePointAccuracyIndicators];
}
if ([self.delegate respondsToSelector:@selector(centerPinMapViewController:didChangeValidZoomScaleTo:)]) {
[self.delegate centerPinMapViewController:self didChangeValidZoomScaleTo:currentZoomStateValid];
}
}
// If the center coordinate has changed, update values
if ((self.centerAnnotaion.coordinate.latitude) != (self.mapView.centerCoordinate.latitude) ||
(self.centerAnnotaion.coordinate.longitude != (self.mapView.centerCoordinate.longitude))) {
self.centerAnnotaion.coordinate = mapView.centerCoordinate;
self.selectedPlacemark = nil;
[self moveMapAnnotationToCoordinate:mapView.centerCoordinate];
// If the current zoom state is valid update selected values
if (currentZoomStateValid) {
self.selectedCoordinate = self.mapView.centerCoordinate;
// Schedule geocode if enabled
if (self.shouldReverseGeocode) {
[NSTimer scheduledTimerWithTimeInterval:REVERSE_GEOCODE_DELAY
target:self selector:@selector(reverseGeoCodeAfterTimer:)
userInfo:[self locationForCoordinate:self.selectedCoordinate]
repeats:NO];
}
if ([self.delegate respondsToSelector:@selector(centerPinMapViewController:didChangeSelectedCoordinate:)]) {
[self.delegate centerPinMapViewController:self didChangeSelectedCoordinate:self.mapView.centerCoordinate];
}
}
}
}
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
if (self.zoomToUser) {
[self changeRegionToCoordinate:userLocation.coordinate withSize:self.zoomMapSize];
self.zoomToUser = NO;
}
if ([self.delegate respondsToSelector:@selector(centerPinMapViewController:didUpdateUserLocation:)]) {
[self.delegate centerPinMapViewController:self didUpdateUserLocation:userLocation.location];
}
}
@end