-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
AIRMapMarker.m
393 lines (327 loc) · 14.4 KB
/
AIRMapMarker.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
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#import "AIRMapMarker.h"
#import <React/RCTBridge.h>
#import <React/RCTEventDispatcher.h>
#import <React/RCTImageLoaderProtocol.h>
#import <React/RCTUtils.h>
#import <React/UIView+React.h>
NSInteger const AIR_CALLOUT_OPEN_ZINDEX_BASELINE = 999;
@implementation AIREmptyCalloutBackgroundView
@end
@implementation AIRMapMarker {
BOOL _hasSetCalloutOffset;
RCTImageLoaderCancellationBlock _reloadImageCancellationBlock;
MKMarkerAnnotationView *_markerView;
MKPinAnnotationView *_pinView;
BOOL _calloutIsOpen;
NSInteger _zIndexBeforeOpen;
BOOL _useLegacyPinView;
}
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self.layer addObserver:self forKeyPath:@"zPosition" options:NSKeyValueObservingOptionNew context:nil];
}
return self;
}
- (void)reactSetFrame:(CGRect)frame
{
// Make sure we use the image size when available
CGSize size = self.image ? self.image.size : frame.size;
CGRect bounds = {CGPointZero, size};
// The MapView is basically in charge of figuring out the center position of the marker view. If the view changed in
// height though, we need to compensate in such a way that the bottom of the marker stays at the same spot on the
// map.
CGFloat dy = (bounds.size.height - self.bounds.size.height) / 2;
CGPoint center = (CGPoint){ self.center.x, self.center.y - dy };
// Avoid crashes due to nan coords
if (isnan(center.x) || isnan(center.y) ||
isnan(bounds.origin.x) || isnan(bounds.origin.y) ||
isnan(bounds.size.width) || isnan(bounds.size.height)) {
RCTLogError(@"Invalid layout for (%@)%@. position: %@. bounds: %@",
self.reactTag, self, NSStringFromCGPoint(center), NSStringFromCGRect(bounds));
return;
}
self.center = center;
self.bounds = bounds;
}
- (void)insertReactSubview:(id<RCTComponent>)subview atIndex:(NSInteger)atIndex {
if ([subview isKindOfClass:[AIRMapCallout class]]) {
self.calloutView = (AIRMapCallout *)subview;
} else {
[super insertReactSubview:(UIView *)subview atIndex:atIndex];
}
}
- (void)removeReactSubview:(id<RCTComponent>)subview {
if ([subview isKindOfClass:[AIRMapCallout class]] && self.calloutView == subview) {
self.calloutView = nil;
} else {
[super removeReactSubview:(UIView *)subview];
}
}
- (MKAnnotationView *)getAnnotationView
{
if ([self shouldUsePinView]) {
// In this case, we want to render a platform "default" legacy marker.
if (_pinView == nil && _useLegacyPinView) {
_pinView = [[MKPinAnnotationView alloc] initWithAnnotation:self reuseIdentifier: nil];
[self addGestureRecognizerToView:_pinView];
_pinView.annotation = self;
if ([_pinView respondsToSelector:@selector(setPinTintColor:)]) {
_pinView.pinTintColor = self.pinColor;
}
_pinView.draggable = self.draggable;
_pinView.layer.zPosition = self.zIndex;
return _pinView;
}
if (_markerView == nil && !_useLegacyPinView) {
_markerView = [[MKMarkerAnnotationView alloc] initWithAnnotation:self reuseIdentifier: nil];
[self addGestureRecognizerToView:_markerView];
_markerView.annotation = self;
_markerView.draggable = self.draggable;
_markerView.layer.zPosition = self.zIndex;
_markerView.markerTintColor = self.pinColor;
_markerView.titleVisibility = self.titleVisibility ?: MKFeatureVisibilityHidden;
_markerView.subtitleVisibility = self.subtitleVisibility ?: MKFeatureVisibilityHidden;
}
return _markerView ?: _pinView;
} else {
// If it has subviews, it means we are wanting to render a custom marker with arbitrary react views.
// if it has a non-null image, it means we want to render a custom marker with the image.
// In either case, we want to return the AIRMapMarker since it is both an MKAnnotation and an
// MKAnnotationView all at the same time.
self.layer.zPosition = self.zIndex;
return self;
}
}
- (void)fillCalloutView:(SMCalloutView *)calloutView
{
// Set everything necessary on the calloutView before it becomes visible.
// Apply the MKAnnotationView's desired calloutOffset (from the top-middle of the view)
if ([self shouldUsePinView] && !_hasSetCalloutOffset && _useLegacyPinView) {
calloutView.calloutOffset = CGPointMake(-8,0);
} else {
calloutView.calloutOffset = self.calloutOffset;
}
if (self.calloutView) {
calloutView.title = nil;
calloutView.subtitle = nil;
if (self.calloutView.tooltip) {
// if tooltip is true, then the user wants their react view to be the "tooltip" as wwell, so we set
// the background view to something empty/transparent
calloutView.backgroundView = [AIREmptyCalloutBackgroundView new];
} else {
// the default tooltip look is wanted, and the user is just filling the content with their react subviews.
// as a result, we use the default "masked" background view.
calloutView.backgroundView = [SMCalloutMaskedBackgroundView new];
}
// when this is set, the callout's content will be whatever react views the user has put as the callout's
// children.
calloutView.contentView = self.calloutView;
} else {
// if there is no calloutView, it means the user wants to use the default callout behavior with title/subtitle
// pairs.
calloutView.title = self.title;
calloutView.subtitle = self.subtitle;
calloutView.contentView = nil;
calloutView.backgroundView = [SMCalloutMaskedBackgroundView new];
}
}
- (void)showCalloutView
{
_calloutIsOpen = YES;
[self setZIndex:_zIndexBeforeOpen];
MKAnnotationView *annotationView = [self getAnnotationView];
[self setSelected:YES animated:NO];
[self.map selectAnnotation:self animated:NO];
id event = @{
@"action": @"marker-select",
@"id": self.identifier ?: @"unknown",
@"coordinate": @{
@"latitude": @(self.coordinate.latitude),
@"longitude": @(self.coordinate.longitude)
}
};
if (self.map.onMarkerSelect) self.map.onMarkerSelect(event);
if (self.onSelect) self.onSelect(event);
if (![self shouldShowCalloutView]) {
// no callout to show
return;
}
[self fillCalloutView:self.map.calloutView];
// This is where we present our custom callout view... MapKit's built-in callout doesn't have the flexibility
// we need, but a lot of work was done by Nick Farina to make this identical to MapKit's built-in.
[self.map.calloutView presentCalloutFromRect:annotationView.bounds
inView:annotationView
constrainedToView:self.map
animated:YES];
}
#pragma mark - Tap Gesture & Events.
- (void)addTapGestureRecognizer {
[self addGestureRecognizerToView:nil];
}
- (void)addGestureRecognizerToView:(UIView *)view {
if (!view) {
view = self;
}
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(_handleTap:)];
// setting this to NO allows the parent MapView to continue receiving marker selection events
tapGestureRecognizer.cancelsTouchesInView = NO;
[view addGestureRecognizer:tapGestureRecognizer];
}
- (void)_handleTap:(UITapGestureRecognizer *)recognizer {
AIRMapMarker *marker = self;
if (!marker) return;
if (marker.selected) {
CGPoint touchPoint = [recognizer locationInView:marker.map.calloutView];
CGRect bubbleFrame = [self.calloutView convertRect:marker.map.calloutView.bounds toView:marker.map];
CGPoint touchPointReal = [recognizer locationInView:self.calloutView];
UIView *calloutView = [marker.map.calloutView hitTest:touchPoint withEvent:nil];
if (calloutView) {
// the callout (or its subview) got clicked, not the marker
UIWindow* win = [[[UIApplication sharedApplication] windows] firstObject];
AIRMapCalloutSubview* calloutSubview = nil;
UIView* tmp = calloutView;
while (tmp && tmp != win && tmp != self.calloutView && tmp != self.map) {
if ([tmp respondsToSelector:@selector(onPress)]) {
calloutSubview = (AIRMapCalloutSubview*) tmp;
break;
}
tmp = tmp.superview;
}
id event = @{
@"action": calloutSubview ? @"callout-inside-press" : @"callout-press",
@"id": marker.identifier ?: @"unknown",
@"point": @{
@"x": @(touchPointReal.x),
@"y": @(touchPointReal.y),
},
@"frame": @{
@"x": @(bubbleFrame.origin.x),
@"y": @(bubbleFrame.origin.y),
@"width": @(bubbleFrame.size.width),
@"height": @(bubbleFrame.size.height),
}
};
if (calloutSubview) calloutSubview.onPress(event);
if (marker.onCalloutPress) marker.onCalloutPress(event);
if (marker.calloutView && marker.calloutView.onPress) marker.calloutView.onPress(event);
if (marker.map.onCalloutPress) marker.map.onCalloutPress(event);
return;
}
}
// the actual marker got clicked
CGPoint touchPointReal = [recognizer locationInView:self.calloutView];
id event = @{
@"action": @"marker-press",
@"id": marker.identifier ?: @"unknown",
@"coordinate": @{
@"latitude": @(marker.coordinate.latitude),
@"longitude": @(marker.coordinate.longitude)
},
@"position": @{
@"x": @(touchPointReal.x),
@"y": @(touchPointReal.y),
}
};
if (marker.onPress) marker.onPress(event);
if (marker.map.onMarkerPress) marker.map.onMarkerPress(event);
[marker.map selectAnnotation:marker animated:NO];
}
- (void)hideCalloutView
{
_calloutIsOpen = NO;
[self setZIndex:_zIndexBeforeOpen];
// hide the callout view
[self.map.calloutView dismissCalloutAnimated:YES];
[self setSelected:NO animated:NO];
[self.map deselectAnnotation:self animated:NO];
id event = @{
@"action": @"marker-deselect",
@"id": self.identifier ?: @"unknown",
@"coordinate": @{
@"latitude": @(self.coordinate.latitude),
@"longitude": @(self.coordinate.longitude)
}
};
if (self.map.onMarkerDeselect) self.map.onMarkerDeselect(event);
if (self.onDeselect) self.onDeselect(event);
}
- (void)setCalloutOffset:(CGPoint)calloutOffset
{
_hasSetCalloutOffset = YES;
[super setCalloutOffset:calloutOffset];
}
- (BOOL)shouldShowCalloutView
{
return self.calloutView != nil || self.title != nil || self.subtitle != nil;
}
- (BOOL)shouldUsePinView
{
return self.reactSubviews.count == 0 && !self.imageSrc;
}
- (void)setOpacity:(double)opacity
{
[self setAlpha:opacity];
}
- (void)setImageSrc:(NSString *)imageSrc
{
_imageSrc = imageSrc;
if (_reloadImageCancellationBlock) {
_reloadImageCancellationBlock();
_reloadImageCancellationBlock = nil;
}
_reloadImageCancellationBlock = [[_bridge moduleForName:@"ImageLoader"] loadImageWithURLRequest:[RCTConvert NSURLRequest:_imageSrc]
size:self.bounds.size
scale:RCTScreenScale()
clipped:YES
resizeMode:RCTResizeModeCenter
progressBlock:nil
partialLoadBlock:nil
completionBlock:^(NSError *error, UIImage *image) {
if (error) {
// TODO(lmr): do something with the error?
NSLog(@"%@", error);
}
dispatch_async(dispatch_get_main_queue(), ^{
self.image = image;
});
}];
}
- (void)setPinColor:(UIColor *)pinColor
{
_pinColor = pinColor;
if(_useLegacyPinView && [_pinView respondsToSelector:@selector(setPinTintColor:)]) {
_pinView.pinTintColor = _pinColor;
} else {
_markerView.markerTintColor = _pinColor;
}
}
- (void)setZIndex:(NSInteger)zIndex
{
_zIndexBeforeOpen = zIndex;
_zIndex = _calloutIsOpen ? zIndex + AIR_CALLOUT_OPEN_ZINDEX_BASELINE : zIndex;
self.layer.zPosition = zIndex;
}
- (BOOL)isSelected {
return _isPreselected || [super isSelected];
}
- (void)dealloc {
[self.layer removeObserver:self forKeyPath:@"zPosition"];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
if ([keyPath isEqualToString:@"zPosition"]) {
self.layer.zPosition = _zIndex;
}
}
- (void)setUseLegacyPinView:(BOOL)value {
_useLegacyPinView = value;
}
@end