forked from react-native-maps/react-native-maps
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMapMarker.tsx
446 lines (396 loc) · 12.5 KB
/
MapMarker.tsx
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
import * as React from 'react';
import {
StyleSheet,
Animated,
Image,
ViewProps,
ImageURISource,
ImageRequireSource,
} from 'react-native';
import decorateMapComponent, {
MapManagerCommand,
NativeComponent,
ProviderContext,
SUPPORTED,
UIManagerCommand,
USES_DEFAULT_IMPLEMENTATION,
} from './decorateMapComponent';
import {
Commands,
MapMarkerNativeComponentType,
} from './MapMarkerNativeComponent';
import {
CalloutPressEvent,
LatLng,
MarkerDeselectEvent,
MarkerDragEvent,
MarkerDragStartEndEvent,
MarkerPressEvent,
MarkerSelectEvent,
Point,
} from './sharedTypes';
import {Modify} from './sharedTypesInternal';
export type MapMarkerProps = ViewProps & {
/**
* Sets the anchor point for the marker.
* The anchor specifies the point in the icon image that is anchored to the marker's position on the Earth's surface.
*
* The anchor point is specified in the continuous space [0.0, 1.0] x [0.0, 1.0],
* where (0, 0) is the top-left corner of the image, and (1, 1) is the bottom-right corner.
*
* The anchoring point in a W x H image is the nearest discrete grid point in a (W + 1) x (H + 1) grid, obtained by scaling the then rounding.
* For example, in a 4 x 2 image, the anchor point (0.7, 0.6) resolves to the grid point at (3, 1).
*
* @default {x: 0.5, y: 1.0}
* @platform iOS: Google Maps only. For Apple Maps, see the `centerOffset` prop
* @platform Android: Supported
*/
anchor?: Point;
/**
* Specifies the point in the marker image at which to anchor the callout when it is displayed.
* This is specified in the same coordinate system as the anchor.
*
* See the `anchor` prop for more details.
*
* @default {x: 0.5, y: 0.0}
* @platform iOS: Google Maps only. For Apple Maps, see the `calloutOffset` prop
* @platform Android: Supported
*/
calloutAnchor?: Point;
/**
* The offset (in points) at which to place the callout bubble.
* When this property is set to (0, 0),
* the anchor point of the callout bubble is placed on the top-center point of the marker view’s frame.
*
* Specifying positive offset values moves the callout bubble down and to the right,
* while specifying negative values moves it up and to the left
*
* @default {x: 0.0, y: 0.0}
* @platform iOS: Apple Maps only. For Google Maps, see the `calloutAnchor` prop
* @platform Android: Not supported. See see the `calloutAnchor` prop
*/
calloutOffset?: Point;
/**
* The offset (in points) at which to display the annotation view.
*
* By default, the center point of an annotation view is placed at the coordinate point of the associated annotation.
*
* Positive offset values move the annotation view down and to the right, while negative values move it up and to the left.
*
* @default {x: 0.0, y: 0.0}
* @platform iOS: Apple Maps only. For Google Maps, see the `anchor` prop
* @platform Android: Not supported. See see the `anchor` prop
*/
centerOffset?: Point;
/**
* The coordinate for the marker.
*
* @platform iOS: Supported
* @platform Android: Supported
*/
coordinate: LatLng;
/**
* The description of the marker.
*
* This is only used if the <Marker /> component has no children that are a `<Callout />`,
* in which case the default callout behavior will be used,
* which will show both the `title` and the `description`, if provided.
*
* @platform iOS: Supported
* @platform Android: Supported
*/
description?: string;
/**
* if `true` allows the marker to be draggable (re-positioned).
*
* @default false
* @platform iOS: Supported
* @platform Android: Supported
*/
draggable?: boolean;
/**
* Sets whether this marker should be flat against the map true or a billboard facing the camera.
*
* @default false
* @platform iOS: Google Maps only
* @platform Android: Supported
*/
flat?: boolean;
/**
* Marker icon to render (equivalent to `icon` property of GMSMarker Class).
* Only local image resources are allowed to be used.
*
* @platform iOS: Google Maps only
* @platform Android: Supported
*/
icon?: ImageURISource | ImageRequireSource;
/**
* A string that can be used to identify this marker.
*
* @platform iOS: Supported
* @platform Android: Supported
*/
identifier?: string;
/**
* A custom image to be used as the marker's icon. Only local image resources are allowed to be used.
*
* @platform iOS: Supported
* @platform Android: Supported
*/
image?: ImageURISource | ImageRequireSource;
/**
* When true, the marker will be pre-selected.
* Setting this to true allows the user to drag the marker without needing to tap on it first to focus it.
*
* @default false
* @platform iOS: Apple Maps only
* @platform Android: Not supported
*/
isPreselected?: boolean;
/**
* Callback that is called when the user taps the callout view.
*
* @platform iOS: Apple Maps only
* @platform Android: Supported
*/
onCalloutPress?: (event: CalloutPressEvent) => void;
/**
* Callback that is called when the marker is deselected, before the callout is hidden.
*
* @platform iOS: Apple Maps only
* @platform Android: Not supported
*/
onDeselect?: (event: MarkerDeselectEvent) => void;
/**
* Callback called continuously as the marker is dragged
*
* @platform iOS: Apple Maps only
* @platform Android: Supported
*/
onDrag?: (event: MarkerDragEvent) => void;
/**
* Callback that is called when a drag on the marker finishes.
* This is usually the point you will want to setState on the marker's coordinate again
*
* @platform iOS: Apple Maps only
* @platform Android: Supported
*/
onDragEnd?: (event: MarkerDragStartEndEvent) => void;
/**
* Callback that is called when the user initiates a drag on the marker (if it is draggable)
*
* @platform iOS: Apple Maps only
* @platform Android: Supported
*/
onDragStart?: (event: MarkerDragStartEndEvent) => void;
/**
* Callback that is called when the marker is tapped by the user.
*
* @platform iOS: Supported
* @platform Android: Supported
*/
onPress?: (event: MarkerPressEvent) => void;
/**
* Callback that is called when the marker becomes selected.
* This will be called when the callout for that marker is about to be shown.
*
* @platform iOS: Apple Maps only.
* @platform Android: Not supported
*/
onSelect?: (event: MarkerSelectEvent) => void;
/**
* The marker's opacity between 0.0 and 1.0.
*
* @default 1.0
* @platform iOS: Supported
* @platform Android: Supported
*/
opacity?: number;
/**
* If no custom marker view or custom image is provided, the platform default pin will be used, which can be customized by this color.
* Ignored if a custom marker is being used.<br/><br/>
* For Android, the set of available colors is limited. Unsupported colors will fall back to red.
* See [#887](https://github.com/react-community/react-native-maps/issues/887) for more information.
*
* @platform iOS: Supported
* @platform Android: Supported
*/
pinColor?: string;
/**
* A float number indicating marker's rotation angle, in degrees.
*
* @default 0
* @platform iOS: Google Maps only
* @platform Android: Supported
*/
rotation?: number;
/**
* Sets whether this marker should propagate `onPress` events.
* Enabling it will stop the parent `MapView`'s `onPress` from being called.
*
* Android does not propagate `onPress` events.
*
* See [#1132](https://github.com/react-community/react-native-maps/issues/1132) for more information.
*
* @default false
* @platform iOS: Apple Maps only
* @platform Android: Not supported
*/
stopPropagation?: boolean;
/**
* Sets whether marker should be tappable.
* If set to false, the marker will not have onPress events.
*
* @default true
* @platform iOS: Google Maps only
* @platform Android: Not supported
*/
tappable?: boolean;
/**
* The title of the marker.
* This is only used if the <Marker /> component has no `<Callout />` children.
*
* If the marker has <Callout /> children, default callout behavior will be used,
* which will show both the `title` and the `description`, if provided.
*
* @platform iOS: Supported
* @platform Android: Supported
*/
title?: string;
/**
* Sets whether this marker should track view changes in info window.
* Enabling it will let marker change content of info window after first render pass, but will lead to decreased performance,
* so it's recommended to disable it whenever you don't need it.
* **Note**: iOS Google Maps only.
*
* @default false
* @platform iOS: Google Maps only
* @platform Android: Not supported
*/
tracksInfoWindowChanges?: boolean;
/**
* Sets whether this marker should track view changes.
* It's recommended to turn it off whenever it's possible to improve custom marker performance.
*
* @default true
* @platform iOS: Google Maps only
* @platform Android: Supported
*/
tracksViewChanges?: boolean;
/**
* The order in which this tile overlay is drawn with respect to other overlays.
* An overlay with a larger z-index is drawn over overlays with smaller z-indices.
* The order of overlays with the same z-index is arbitrary.
*
* @platform iOS: Supported
* @platform Android: Supported
*/
zIndex?: number;
};
type OmittedProps = Omit<MapMarkerProps, 'stopPropagation'>;
export type NativeProps = Modify<
OmittedProps,
{icon?: string; image?: MapMarkerProps['image'] | string}
> & {
ref: React.RefObject<MapMarkerNativeComponentType>;
};
export class MapMarker extends React.Component<MapMarkerProps> {
// declaration only, as they are set through decorateMap
declare context: React.ContextType<typeof ProviderContext>;
getNativeComponent!: () => NativeComponent<NativeProps>;
getMapManagerCommand!: (name: string) => MapManagerCommand;
getUIManagerCommand!: (name: string) => UIManagerCommand;
static Animated: Animated.AnimatedComponent<typeof MapMarker>;
private marker: NativeProps['ref'];
constructor(props: MapMarkerProps) {
super(props);
this.marker = React.createRef<MapMarkerNativeComponentType>();
this.showCallout = this.showCallout.bind(this);
this.hideCallout = this.hideCallout.bind(this);
this.setCoordinates = this.setCoordinates.bind(this);
this.redrawCallout = this.redrawCallout.bind(this);
this.animateMarkerToCoordinate = this.animateMarkerToCoordinate.bind(this);
}
setNativeProps(props: Partial<NativeProps>) {
// @ts-ignore
this.marker.current?.setNativeProps(props);
}
showCallout() {
if (this.marker.current) {
Commands.showCallout(this.marker.current);
}
}
hideCallout() {
if (this.marker.current) {
Commands.hideCallout(this.marker.current);
}
}
setCoordinates(coordinate: LatLng) {
if (this.marker.current) {
Commands.setCoordinates(this.marker.current, coordinate);
}
}
redrawCallout() {
if (this.marker.current) {
Commands.redrawCallout(this.marker.current);
}
}
animateMarkerToCoordinate(coordinate: LatLng, duration: number = 500) {
if (this.marker.current) {
Commands.animateMarkerToCoordinate(
this.marker.current,
coordinate,
duration,
);
}
}
redraw() {
if (this.marker.current) {
Commands.redraw(this.marker.current);
}
}
render() {
const {stopPropagation = false} = this.props;
let image;
if (this.props.image) {
image = Image.resolveAssetSource(this.props.image) || {};
image = image.uri || this.props.image;
}
let icon;
if (this.props.icon) {
icon = Image.resolveAssetSource(this.props.icon) || {};
icon = icon.uri;
}
const AIRMapMarker = this.getNativeComponent();
return (
<AIRMapMarker
{...this.props}
ref={this.marker}
image={image}
icon={icon}
style={[styles.marker, this.props.style]}
onPress={event => {
if (stopPropagation) {
event.stopPropagation();
}
if (this.props.onPress) {
this.props.onPress(event);
}
}}
/>
);
}
}
const styles = StyleSheet.create({
marker: {
position: 'absolute',
backgroundColor: 'transparent',
},
});
MapMarker.Animated = Animated.createAnimatedComponent(MapMarker);
export default decorateMapComponent(MapMarker, 'Marker', {
google: {
ios: SUPPORTED,
android: USES_DEFAULT_IMPLEMENTATION,
},
});