-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDBMapSelectorManager.m
477 lines (411 loc) · 19.8 KB
/
DBMapSelectorManager.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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
//
// DBMapSelectorManager.m
// DBMapSelectorViewController
//
// Created by Denis Bogatyrev on 27.03.15.
//
// The MIT License (MIT)
// Copyright (c) 2015 Denis Bogatyrev.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//
#import "DBMapSelectorGestureRecognizer.h"
#import "DBMapSelectorAnnotation.h"
#import "DBMapSelectorOverlay.h"
#import "DBMapSelectorOverlayRenderer.h"
#import "DBMapSelectorManager.h"
static const NSInteger kDefaultRadius = 1000;
static const NSInteger kDefaultMinDistance = 100;
static const NSInteger kDefaultMaxDistance = 10000;
@interface DBMapSelectorManager () {
BOOL _isFirstTimeApplySelectorSettings;
UIView *_radiusTouchView;
UILongPressGestureRecognizer *_longPressGestureRecognizer;
}
@property (strong, nonatomic) DBMapSelectorOverlay *selectorOverlay;
@property (strong, nonatomic) DBMapSelectorOverlayRenderer *selectorOverlayRenderer;
@property (assign, nonatomic) BOOL mapViewGestureEnabled;
@property (assign, nonatomic) MKMapPoint prevMapPoint;
@property (assign, nonatomic) CLLocationDistance prevRadius;
@property (assign, nonatomic) CGRect radiusTouchRect;
@property (strong, nonatomic) NSTimer *zoomRegionTimer;
-(void)resetZoomRegionTimer;
-(void)updateZoomForTouch:(UITouch *)touch;
-(void)updateCircleForDraggingTouch:(UITouch *)touch;
@end
@implementation DBMapSelectorManager
- (instancetype)initWithMapView:(MKMapView *)mapView {
self = [super init];
if (self) {
_isFirstTimeApplySelectorSettings = YES;
_mapView = mapView;
[self prepareForFirstUse];
}
return self;
}
- (void)prepareForFirstUse {
[self selectorSetDefaults];
_selectorOverlay = [[DBMapSelectorOverlay alloc] initWithCenterCoordinate:_circleCoordinate radius:_circleRadius];
#ifdef DEBUG
_radiusTouchView = [[UIView alloc] initWithFrame:CGRectZero];
_radiusTouchView.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:.5f];
_radiusTouchView.userInteractionEnabled = NO;
// [self.mapView addSubview:_radiusTouchView];
#endif
_longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureRecognizer:)];
_mapViewGestureEnabled = YES;
[self.mapView addGestureRecognizer:[self selectorGestureRecognizer]];
}
#pragma mark Defaults
- (void)selectorSetDefaults {
self.editingType = DBMapSelectorEditingTypeFull;
self.circleRadius = kDefaultRadius;
self.circleRadiusMin = kDefaultMinDistance;
self.circleRadiusMax = kDefaultMaxDistance;
self.hidden = NO;
self.fillInside = YES;
self.shouldShowRadiusText = YES;
self.fillColor = [UIColor orangeColor];
self.strokeColor = [UIColor darkGrayColor];
self.pointColor = [UIColor greenColor];
self.textColor = [UIColor redColor];
self.lineColor = [UIColor blueColor];
self.centerPinColor = [UIColor cyanColor];
self.mapRegionCoef = 2.f;
}
- (void)applySelectorSettings {
[self updateMapRegionForMapSelector];
[self displaySelectorAnnotationIfNeeded];
[self recalculateRadiusTouchRect];
if (_isFirstTimeApplySelectorSettings) {
_isFirstTimeApplySelectorSettings = NO;
[self.mapView removeOverlay:_selectorOverlay];
[self.mapView addOverlay:_selectorOverlay];
}
}
#pragma mark - GestureRecognizer
- (DBMapSelectorGestureRecognizer *)selectorGestureRecognizer {
__weak typeof(self) weakSelf = self;
DBMapSelectorGestureRecognizer *selectorGestureRecognizer = [[DBMapSelectorGestureRecognizer alloc] init];
selectorGestureRecognizer.touchesBeganCallback = ^(NSSet * touches, UIEvent * event) {
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:weakSelf.mapView];
// NSLog(@"---- %@", CGRectContainsPoint(weakSelf.selectorRadiusRect, p) ? @"Y" : @"N");
CLLocationCoordinate2D coord = [weakSelf.mapView convertPoint:touchPoint toCoordinateFromView:weakSelf.mapView];
MKMapPoint mapPoint = MKMapPointForCoordinate(coord);
if (CGRectContainsPoint(weakSelf.radiusTouchRect, touchPoint) && weakSelf.selectorOverlay.editingRadius && !weakSelf.hidden){
[[NSNotificationCenter defaultCenter] postNotificationName:DBMapSelectorCircleResizeDidBeginNotificationName object:weakSelf];
if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(mapSelectorManagerWillBeginHandlingUserInteraction:)]) {
[weakSelf.delegate mapSelectorManagerWillBeginHandlingUserInteraction:weakSelf];
}
[weakSelf updateZoomForTouch:touch];
dispatch_async(dispatch_get_main_queue(), ^{
weakSelf.mapView.scrollEnabled = NO;
weakSelf.mapView.userInteractionEnabled = NO;
weakSelf.mapViewGestureEnabled = NO;
});
} else {
weakSelf.mapView.scrollEnabled = YES;
weakSelf.mapView.userInteractionEnabled = YES;
}
weakSelf.prevMapPoint = mapPoint;
weakSelf.prevRadius = weakSelf.circleRadius;
};
selectorGestureRecognizer.touchesMovedCallback = ^(NSSet * touches, UIEvent * event) {
if(!weakSelf.mapViewGestureEnabled && [event allTouches].count == 1){
UITouch *touch = [touches anyObject];
[weakSelf updateZoomForTouch:touch];
[weakSelf updateCircleForDraggingTouch:touch];
}
};
selectorGestureRecognizer.touchesEndedCallback = ^(NSSet * touches, UIEvent * event) {
weakSelf.mapView.scrollEnabled = YES;
weakSelf.mapView.userInteractionEnabled = YES;
if (weakSelf.prevRadius != weakSelf.circleRadius) {
[weakSelf recalculateRadiusTouchRect];
// if (((weakSelf.prevRadius / weakSelf.circleRadius) >= 1.25f) || ((weakSelf.prevRadius / weakSelf.circleRadius) <= .75f)) {
[weakSelf updateMapRegionForMapSelector];
// }
}
if(!weakSelf.mapViewGestureEnabled) {
[self resetZoomRegionTimer];
[[NSNotificationCenter defaultCenter] postNotificationName:DBMapSelectorCircleResizeDidEndNotificationName object:self];
if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(mapSelectorManagerDidHandleUserInteraction:)]) {
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf.delegate mapSelectorManagerDidHandleUserInteraction:weakSelf];
});
}
}
weakSelf.mapViewGestureEnabled = YES;
};
return selectorGestureRecognizer;
}
- (void)longPressGestureRecognizer:(UILongPressGestureRecognizer *)gestureRecognizer {
if ([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]] &&
( self.editingType == DBMapSelectorEditingTypeFull || self.editingType == DBMapSelectorEditingTypeCoordinateOnly )) {
switch (gestureRecognizer.state) {
case UIGestureRecognizerStateBegan: {
CGPoint touchPoint = [gestureRecognizer locationInView:self.mapView];
CLLocationCoordinate2D coord = [self.mapView convertPoint:touchPoint toCoordinateFromView:self.mapView];
self.circleCoordinate = coord;
[self displaySelectorAnnotationIfNeeded];
break;
}
case UIGestureRecognizerStateEnded:
if (NO == MKMapRectContainsRect(self.mapView.visibleMapRect, _selectorOverlay.boundingMapRect)) {
[self updateMapRegionForMapSelector];
}
break;
default:
break;
}
}
}
#pragma mark - Accessors
-(void)resetZoomRegionTimer{
[self.zoomRegionTimer invalidate];
self.zoomRegionTimer = nil;
}
- (void)setCircleRadius:(CLLocationDistance)circleRadius {
if (_circleRadius!= MAX(MIN(circleRadius, _circleRadiusMax), _circleRadiusMin)) {
_circleRadius = MAX(MIN(circleRadius, _circleRadiusMax), _circleRadiusMin);
_selectorOverlay.radius = _circleRadius;
if (_delegate && [_delegate respondsToSelector:@selector(mapSelectorManager:didChangeRadius:)]) {
[_delegate mapSelectorManager:self
didChangeRadius:_circleRadius];
}
}
}
- (void)setCircleRadiusMax:(CLLocationDistance)circleRadiusMax {
if (_circleRadiusMax != circleRadiusMax) {
_circleRadiusMax = circleRadiusMax;
_circleRadiusMin = MIN(_circleRadiusMin, _circleRadiusMax);
self.circleRadius = _circleRadius;
}
}
- (void)setCircleRadiusMin:(CLLocationDistance)circleRadiusMin {
if (_circleRadiusMin != circleRadiusMin) {
_circleRadiusMin = circleRadiusMin;
_circleRadiusMax = MAX(_circleRadiusMax, _circleRadiusMin);
self.circleRadius = _circleRadius;
}
}
- (void)setCircleCoordinate:(CLLocationCoordinate2D)circleCoordinate {
if ((_circleCoordinate.latitude != circleCoordinate.latitude) || (_circleCoordinate.longitude != circleCoordinate.longitude)) {
_circleCoordinate = circleCoordinate;
[self.mapView removeOverlay:_selectorOverlay];
_selectorOverlay.coordinate = _circleCoordinate;
if (_hidden == NO) {
[self.mapView addOverlay:_selectorOverlay];
}
[self recalculateRadiusTouchRect];
if (_delegate && [_delegate respondsToSelector:@selector(mapSelectorManager:didChangeCoordinate:)]) {
[_delegate mapSelectorManager:self
didChangeCoordinate:_circleCoordinate];
}
}
}
- (void)setFillColor:(UIColor *)fillColor {
if (_fillColor != fillColor) {
_fillColor = fillColor;
_selectorOverlayRenderer.fillColor = fillColor;
[_selectorOverlayRenderer invalidatePath];
}
}
- (void)setStrokeColor:(UIColor *)strokeColor {
if (_strokeColor != strokeColor) {
_strokeColor = strokeColor;
_selectorOverlayRenderer.strokeColor = strokeColor;
[_selectorOverlayRenderer invalidatePath];
}
}
-(void)setInfoColor:(UIColor *)pointColor{
if(_pointColor != pointColor){
_pointColor = pointColor;
_selectorOverlayRenderer.pointColor = pointColor;
[_selectorOverlayRenderer invalidatePath];
}
}
-(void)setTextColor:(UIColor *)textColor{
if(_textColor != textColor){
_textColor = textColor;
_selectorOverlayRenderer.textColor = textColor;
[_selectorOverlayRenderer invalidatePath];
}
}
-(void)setLineColor:(UIColor *)lineColor{
if(_lineColor != lineColor){
_lineColor = lineColor;
_selectorOverlayRenderer.lineColor = lineColor;
[_selectorOverlayRenderer invalidatePath];
}
}
- (void)setEditingType:(DBMapSelectorEditingType)editingType {
if (_editingType != editingType) {
_editingType = editingType;
_selectorOverlay.editingCoordinate = (_editingType == DBMapSelectorEditingTypeCoordinateOnly || _editingType == DBMapSelectorEditingTypeFull);
_selectorOverlay.editingRadius = (_editingType == DBMapSelectorEditingTypeRadiusOnly || _editingType == DBMapSelectorEditingTypeFull);
[self displaySelectorAnnotationIfNeeded];
}
}
- (void)setHidden:(BOOL)hidden {
if (_hidden != hidden) {
_hidden = hidden;
[self displaySelectorAnnotationIfNeeded];
if (_hidden) {
[self.mapView removeOverlay:_selectorOverlay];
} else {
[self.mapView addOverlay:_selectorOverlay];
}
[self recalculateRadiusTouchRect];
}
}
- (void)setFillInside:(BOOL)fillInside {
if (_fillInside != fillInside) {
_fillInside = fillInside;
_selectorOverlay.fillInside = fillInside;
}
}
- (void)setShouldShowRadiusText:(BOOL)shouldShowRadiusText {
_selectorOverlay.shouldShowRadiusText = shouldShowRadiusText;
}
- (void)setShouldLongPressGesture:(BOOL)shouldLongPressGesture {
if (_shouldLongPressGesture != shouldLongPressGesture) {
_shouldLongPressGesture = shouldLongPressGesture;
if (_shouldLongPressGesture) {
[self.mapView addGestureRecognizer:_longPressGestureRecognizer];
} else {
[self.mapView removeGestureRecognizer:_longPressGestureRecognizer];
}
}
}
#pragma mark - Additional
-(void)updateZoomForTouch:(UITouch *)touch{
CGPoint touchPoint = [touch locationInView:self.mapView];
static const NSUInteger autoZoomThreshold = 40;
#define shouldZoomOut (self.mapView.frame.size.width - touchPoint.x < autoZoomThreshold && self.circleRadius < self.circleRadiusMax)
#define shouldZoomIn (touchPoint.x - [self.mapView convertCoordinate:self.circleCoordinate toPointToView:self.mapView].x < autoZoomThreshold * 2 && self.circleRadius > self.circleRadiusMin)
if(shouldZoomIn || shouldZoomOut){
if(!self.zoomRegionTimer){
self.zoomRegionTimer = [NSTimer scheduledTimerWithTimeInterval:.5 repeats:NO block:^(NSTimer * __nonnull timer) {
self.zoomRegionTimer = [NSTimer scheduledTimerWithTimeInterval:.05 repeats:YES block:^(NSTimer * __nonnull timer) {
CLLocationDegrees delta = self.circleRadius / 10 / 111111;
MKCoordinateRegion currentRegion = MKCoordinateRegionForMapRect(self.mapView.visibleMapRect);
if(shouldZoomIn){
currentRegion.span.longitudeDelta -= currentRegion.span.longitudeDelta / 20;
currentRegion.span.latitudeDelta = 0;
}
else if(shouldZoomOut){
currentRegion.span.longitudeDelta += delta;
}
else{
[self resetZoomRegionTimer];
}
[self.mapView setRegion:currentRegion animated:NO];
[self updateCircleForDraggingTouch:touch];
}];
}];
}
}
else if(self.zoomRegionTimer){
[self resetZoomRegionTimer];
}
#undef shouldZoomOut
#undef shouldZoomIn
}
-(void)updateCircleForDraggingTouch:(UITouch *)touch{
CGPoint point = [touch locationInView:self.mapView];
CLLocationCoordinate2D coord = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];
MKMapPoint mapPoint = MKMapPointForCoordinate(coord);
double meterDistance = (mapPoint.x - self.prevMapPoint.x)/MKMapPointsPerMeterAtLatitude(self.mapView.centerCoordinate.latitude) + self.prevRadius;
self.circleRadius = MIN( MAX( meterDistance, self.circleRadiusMin ), self.circleRadiusMax );
}
- (void)recalculateRadiusTouchRect {
MKMapRect selectorMapRect = _selectorOverlay.boundingMapRect;
MKMapPoint selectorRadiusPoint = MKMapPointMake(MKMapRectGetMaxX(selectorMapRect), MKMapRectGetMidY(selectorMapRect));
MKCoordinateRegion coordinateRegion = MKCoordinateRegionMakeWithDistance(MKCoordinateForMapPoint(selectorRadiusPoint), _circleRadius *.3f, _circleRadius *.3f);
BOOL needDisplay = MKMapRectContainsPoint(self.mapView.visibleMapRect, selectorRadiusPoint) && (_hidden == NO);
_radiusTouchRect = needDisplay ? [self.mapView convertRegion:coordinateRegion toRectToView:self.mapView] : CGRectZero;
#ifdef DEBUG
_radiusTouchView.frame = _radiusTouchRect;
_radiusTouchView.hidden = !needDisplay;
#endif
}
- (void)updateMapRegionForMapSelector {
MKCoordinateRegion selectorRegion = MKCoordinateRegionForMapRect(_selectorOverlay.boundingMapRect);
MKCoordinateRegion region;
region.center = selectorRegion.center;
region.span = MKCoordinateSpanMake(selectorRegion.span.latitudeDelta * _mapRegionCoef, selectorRegion.span.longitudeDelta * _mapRegionCoef);
[self.mapView setRegion:region animated:YES];
}
- (void)displaySelectorAnnotationIfNeeded {
for (id<MKAnnotation> annotation in self.mapView.annotations) {
if ([annotation isKindOfClass:[DBMapSelectorAnnotation class]]) {
[self.mapView removeAnnotation:annotation];
}
}
if (_hidden == NO && ((_editingType == DBMapSelectorEditingTypeFull) || (_editingType == DBMapSelectorEditingTypeCoordinateOnly))) {
DBMapSelectorAnnotation *selectorAnnotation = [[DBMapSelectorAnnotation alloc] init];
selectorAnnotation.coordinate = _circleCoordinate;
[self.mapView addAnnotation:selectorAnnotation];
}
}
#pragma mark - MKMapView Delegate
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
if ([annotation isKindOfClass:[DBMapSelectorAnnotation class]]) {
static NSString *selectorIdentifier = @"DBMapSelectorAnnotationView";
MKPinAnnotationView *selectorAnnotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:selectorIdentifier];
if (selectorAnnotationView) {
selectorAnnotationView.annotation = annotation;
} else {
selectorAnnotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:selectorIdentifier];
selectorAnnotationView.pinTintColor = self.centerPinColor;
selectorAnnotationView.draggable = YES;
}
selectorAnnotationView.selected = YES;
return selectorAnnotationView;
} else {
return nil;
}
}
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)annotationView didChangeDragState:(MKAnnotationViewDragState)newState fromOldState:(MKAnnotationViewDragState)oldState {
if (newState == MKAnnotationViewDragStateStarting) {
_mapViewGestureEnabled = YES;
}
if (newState == MKAnnotationViewDragStateEnding) {
self.circleCoordinate = annotationView.annotation.coordinate;
if (NO == MKMapRectContainsRect(mapView.visibleMapRect, _selectorOverlay.boundingMapRect)) {
[self performSelector:@selector(updateMapRegionForMapSelector)
withObject:nil
afterDelay:.3f];
}
}
}
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay {
MKOverlayRenderer *overlayRenderer;
if ([overlay isKindOfClass:[DBMapSelectorOverlay class]]) {
_selectorOverlayRenderer = [[DBMapSelectorOverlayRenderer alloc] initWithSelectorOverlay:(DBMapSelectorOverlay *)overlay];
_selectorOverlayRenderer.fillColor = _fillColor;
_selectorOverlayRenderer.strokeColor = _strokeColor;
_selectorOverlayRenderer.pointColor = _pointColor;
_selectorOverlayRenderer.textColor = _textColor;
_selectorOverlayRenderer.lineColor = _lineColor;
overlayRenderer = _selectorOverlayRenderer;
} else if ([overlay isKindOfClass:[MKTileOverlay class]]) {
overlayRenderer = [[MKTileOverlayRenderer alloc] initWithTileOverlay:overlay];
}
return overlayRenderer;
}
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
[self recalculateRadiusTouchRect];
}
@end