-
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathAIRGoogleMapMarker.m
456 lines (372 loc) · 14.3 KB
/
AIRGoogleMapMarker.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
//
// AIRGoogleMapMarker.m
// AirMaps
//
// Created by Gil Birman on 9/2/16.
//
#ifdef HAVE_GOOGLE_MAPS
#import "AIRGoogleMapMarker.h"
#import <GoogleMaps/GoogleMaps.h>
#import <React/RCTImageLoaderProtocol.h>
#import <React/RCTUtils.h>
#import "AIRGMSMarker.h"
#import "AIRGoogleMapCallout.h"
#import "AIRDummyView.h"
CGRect unionRect(CGRect a, CGRect b) {
return CGRectMake(
MIN(a.origin.x, b.origin.x),
MIN(a.origin.y, b.origin.y),
MAX(a.size.width, b.size.width),
MAX(a.size.height, b.size.height));
}
@interface AIRGoogleMapMarker ()
@end
@implementation AIRGoogleMapMarker {
RCTImageLoaderCancellationBlock _reloadImageCancellationBlock;
__weak UIImageView *_iconImageView;
UIView *_iconView;
}
- (instancetype)init
{
if ((self = [super init])) {
_realMarker = [[AIRGMSMarker alloc] init];
_realMarker.fakeMarker = self;
_realMarker.tracksViewChanges = true;
_realMarker.tracksInfoWindowChanges = false;
}
return self;
}
- (void)layoutSubviews {
float width = 0;
float height = 0;
for (UIView *v in [_iconView subviews]) {
float fw = v.frame.origin.x + v.frame.size.width;
float fh = v.frame.origin.y + v.frame.size.height;
width = MAX(fw, width);
height = MAX(fh, height);
}
[_iconView setFrame:CGRectMake(0, 0, width, height)];
}
- (void)iconViewInsertSubview:(UIView*)subview atIndex:(NSInteger)atIndex {
if (!_realMarker.iconView) {
_iconView = [[UIView alloc] init];
_realMarker.iconView = _iconView;
}
[_iconView insertSubview:subview atIndex:atIndex];
}
- (void)insertReactSubview:(id<RCTComponent>)subview atIndex:(NSInteger)atIndex {
if ([subview isKindOfClass:[AIRGoogleMapCallout class]]) {
self.calloutView = (AIRGoogleMapCallout *)subview;
} else { // a child view of the marker
[self iconViewInsertSubview:(UIView*)subview atIndex:atIndex+1];
}
AIRDummyView *dummySubview = [[AIRDummyView alloc] initWithView:(UIView *)subview];
[super insertReactSubview:(UIView*)dummySubview atIndex:atIndex];
}
- (void)removeReactSubview:(id<RCTComponent>)dummySubview {
UIView *subview = [dummySubview isKindOfClass:[AIRDummyView class]] ? ((AIRDummyView *)dummySubview).view : (UIView *)dummySubview;
if ([subview isKindOfClass:[AIRGoogleMapCallout class]]) {
self.calloutView = nil;
} else {
[subview removeFromSuperview];
}
[super removeReactSubview:(UIView*)dummySubview];
}
- (void)showCalloutView {
[_realMarker.map setSelectedMarker:_realMarker];
}
- (void)hideCalloutView {
[_realMarker.map setSelectedMarker:Nil];
}
- (void)redraw {
if (!_realMarker.iconView) return;
BOOL oldValue = _realMarker.tracksViewChanges;
if (oldValue == YES)
{
// Immediate refresh, like right now. Not waiting for next frame.
UIView *view = _realMarker.iconView;
_realMarker.iconView = nil;
_realMarker.iconView = view;
}
else
{
// Refresh according to docs
_realMarker.tracksViewChanges = YES;
_realMarker.tracksViewChanges = NO;
}
}
- (UIView *)markerInfoContents {
if (self.calloutView && !self.calloutView.tooltip) {
return self.calloutView;
}
return nil;
}
- (UIView *)markerInfoWindow {
if (self.calloutView && self.calloutView.tooltip) {
return self.calloutView;
}
return nil;
}
- (void)didTapInfoWindowOfMarker:(AIRGMSMarker *)marker point:(CGPoint)point frame:(CGRect)frame {
if (self.calloutView && self.calloutView.onPress) {
//todo: why not 'callout-press' ?
id event = @{
@"action": @"marker-overlay-press",
@"id": self.identifier ?: @"unknown",
@"point": @{
@"x": @(point.x),
@"y": @(point.y),
},
@"frame": @{
@"x": @(frame.origin.x),
@"y": @(frame.origin.y),
@"width": @(frame.size.width),
@"height": @(frame.size.height),
}
};
self.calloutView.onPress(event);
}
}
- (void)didTapInfoWindowOfMarker:(AIRGMSMarker *)marker {
[self didTapInfoWindowOfMarker:marker point:CGPointMake(-1, -1) frame:CGRectZero];
}
- (void)didTapInfoWindowOfMarker:(AIRGMSMarker *)marker subview:(AIRGoogleMapCalloutSubview*)subview point:(CGPoint)point frame:(CGRect)frame {
if (subview && subview.onPress) {
//todo: why not 'callout-inside-press' ?
id event = @{
@"action": @"marker-inside-overlay-press",
@"id": self.identifier ?: @"unknown",
@"point": @{
@"x": @(point.x),
@"y": @(point.y),
},
@"frame": @{
@"x": @(frame.origin.x),
@"y": @(frame.origin.y),
@"width": @(frame.size.width),
@"height": @(frame.size.height),
}
};
subview.onPress(event);
} else {
[self didTapInfoWindowOfMarker:marker point:point frame:frame];
}
}
- (void)didBeginDraggingMarker:(AIRGMSMarker *)marker {
if (!self.onDragStart) return;
self.onDragStart([self makeEventData]);
}
- (void)didEndDraggingMarker:(AIRGMSMarker *)marker {
if (!self.onDragEnd) return;
self.onDragEnd([self makeEventData]);
}
- (void)didDragMarker:(AIRGMSMarker *)marker {
if (!self.onDrag) return;
self.onDrag([self makeEventData]);
}
- (void)setCoordinate:(CLLocationCoordinate2D)coordinate {
_realMarker.position = coordinate;
}
- (CLLocationCoordinate2D)coordinate {
return _realMarker.position;
}
- (void)setRotation:(CLLocationDegrees)rotation {
_realMarker.rotation = rotation;
}
- (CLLocationDegrees)rotation {
return _realMarker.rotation;
}
- (void)setIdentifier:(NSString *)identifier {
_realMarker.identifier = identifier;
}
- (NSString *)identifier {
return _realMarker.identifier;
}
- (void)setOnPress:(RCTBubblingEventBlock)onPress {
_realMarker.onPress = onPress;
}
- (RCTBubblingEventBlock)onPress {
return _realMarker.onPress;
}
- (void)setOnSelect:(RCTDirectEventBlock)onSelect {
_realMarker.onSelect = onSelect;
}
- (RCTDirectEventBlock)onSelect {
return _realMarker.onSelect;
}
- (void)setOnDeselect:(RCTDirectEventBlock)onDeselect {
_realMarker.onDeselect = onDeselect;
}
- (RCTDirectEventBlock)onDeselect {
return _realMarker.onDeselect;
}
- (void)setOpacity:(double)opacity
{
_realMarker.opacity = opacity;
}
- (void)setImageSrc:(NSString *)imageSrc
{
_imageSrc = imageSrc;
if (_reloadImageCancellationBlock) {
_reloadImageCancellationBlock();
_reloadImageCancellationBlock = nil;
}
if (!_imageSrc) {
if (_iconImageView) [_iconImageView removeFromSuperview];
return;
}
if (!_iconImageView) {
// prevent glitch with marker (cf. https://github.com/react-native-maps/react-native-maps/issues/738)
UIImageView *empyImageView = [[UIImageView alloc] init];
_iconImageView = empyImageView;
[self iconViewInsertSubview:_iconImageView atIndex:0];
}
_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(), ^{
// TODO(gil): This way allows different image sizes
if (self->_iconImageView) [self->_iconImageView removeFromSuperview];
// ... but this way is more efficient?
// if (_iconImageView) {
// [_iconImageView setImage:image];
// return;
// }
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
// TODO: w,h or pixel density could be a prop.
float density = 1;
float w = image.size.width/density;
float h = image.size.height/density;
CGRect bounds = CGRectMake(0, 0, w, h);
imageView.contentMode = UIViewContentModeScaleAspectFit;
[imageView setFrame:bounds];
// NOTE: sizeToFit doesn't work instead. Not sure why.
// TODO: Doing it this way is not ideal because it causes things to reshuffle
// when the image loads IF the image is larger than the UIView.
// Shouldn't required images have size info automatically via RN?
CGRect selfBounds = unionRect(bounds, self.bounds);
[self setFrame:selfBounds];
self->_iconImageView = imageView;
[self iconViewInsertSubview:imageView atIndex:0];
});
}];
}
- (void)setIconSrc:(NSString *)iconSrc
{
_iconSrc = iconSrc;
if (_reloadImageCancellationBlock) {
_reloadImageCancellationBlock();
_reloadImageCancellationBlock = nil;
}
if (!_realMarker.icon) {
// prevent glitch with marker (cf. https://github.com/react-native-maps/react-native-maps/issues/3657)
UIImage *emptyImage = [[UIImage alloc] init];
_realMarker.icon = emptyImage;
}
_reloadImageCancellationBlock =
[[_bridge moduleForName:@"ImageLoader"] loadImageWithURLRequest:[RCTConvert NSURLRequest:_iconSrc]
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->_realMarker.icon = image;
});
}];
}
- (void)setTitle:(NSString *)title {
_realMarker.title = [title copy];
}
- (NSString *)title {
return _realMarker.title;
}
- (void)setSubtitle:(NSString *)subtitle {
_realMarker.snippet = subtitle;
}
- (NSString *)subtitle {
return _realMarker.snippet;
}
- (void)setPinColor:(UIColor *)pinColor {
_pinColor = pinColor;
_realMarker.icon = [GMSMarker markerImageWithColor:pinColor];
}
- (void)setAnchor:(CGPoint)anchor {
_anchor = anchor;
_realMarker.groundAnchor = anchor;
}
- (void)setCalloutAnchor:(CGPoint)calloutAnchor {
_calloutAnchor = calloutAnchor;
_realMarker.infoWindowAnchor = calloutAnchor;
}
- (void)setZIndex:(NSInteger)zIndex
{
_zIndex = zIndex;
_realMarker.zIndex = (int)zIndex;
}
- (void)setDraggable:(BOOL)draggable {
_realMarker.draggable = draggable;
}
- (BOOL)draggable {
return _realMarker.draggable;
}
- (void)setTappable:(BOOL)tappable {
_realMarker.tappable = tappable;
}
- (BOOL)tappable {
return _realMarker.tappable;
}
- (void)setFlat:(BOOL)flat {
_realMarker.flat = flat;
}
- (BOOL)flat {
return _realMarker.flat;
}
- (void)setTracksViewChanges:(BOOL)tracksViewChanges {
_realMarker.tracksViewChanges = tracksViewChanges;
}
- (BOOL)tracksViewChanges {
return _realMarker.tracksViewChanges;
}
- (void)setTracksInfoWindowChanges:(BOOL)tracksInfoWindowChanges {
_realMarker.tracksInfoWindowChanges = tracksInfoWindowChanges;
}
- (BOOL)tracksInfoWindowChanges {
return _realMarker.tracksInfoWindowChanges;
}
- (id)makeEventData:(NSString *)action {
CLLocationCoordinate2D coordinate = self.realMarker.position;
CGPoint position = [self.realMarker.map.projection pointForCoordinate:coordinate];
return @{
@"id": self.identifier ?: @"unknown",
@"position": @{
@"x": @(position.x),
@"y": @(position.y),
},
@"coordinate": @{
@"latitude": @(coordinate.latitude),
@"longitude": @(coordinate.longitude),
},
@"action": action,
};
}
- (id)makeEventData {
return [self makeEventData:@"unknown"];
}
@end
#endif