-
-
Notifications
You must be signed in to change notification settings - Fork 816
/
Annotation.tsx
141 lines (118 loc) · 3.46 KB
/
Annotation.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
import React, { ReactElement } from 'react';
import { Animated as RNAnimated, Easing } from 'react-native';
import { Point } from 'geojson';
import Animated from '../utils/animated/Animated';
import { AnimatedPoint } from '../classes';
import { OnPressEvent } from '../types/OnPressEvent';
import { SymbolLayerStyle } from '../Mapbox';
import { SymbolLayer } from './SymbolLayer';
type Props = {
id: string;
animated?: boolean;
animationDuration?: number;
animationEasingFunction?: (x: number) => number;
coordinates: number[];
onPress?: (event: OnPressEvent) => void;
children: ReactElement | ReactElement[];
style?: object;
icon?: string | number | object;
};
type AnnotationState = {
shape: Point | AnimatedPoint | null;
};
class Annotation extends React.Component<Props, AnnotationState> {
static defaultProps = {
animated: false,
animationDuration: 1000,
animationEasingFunction: Easing.linear,
};
constructor(props: Props) {
super(props);
const shape = this._getShapeFromProps(props);
this.state = {
shape: props.animated ? new AnimatedPoint(shape) : shape,
};
this.onPress = this.onPress.bind(this);
}
componentDidUpdate(prevProps: Props) {
if (!Array.isArray(this.props.coordinates)) {
this.setState({ shape: null });
return;
}
const haveCoordinatesChanged =
prevProps.coordinates[0] !== this.props.coordinates[0] ||
prevProps.coordinates[1] !== this.props.coordinates[1];
if (
prevProps.animated !== this.props.animated ||
(haveCoordinatesChanged && (!this.state.shape || !this.props.animated))
) {
const shape = this._getShapeFromProps(this.props);
this.setState({
shape: this.props.animated ? new AnimatedPoint(shape) : shape,
});
} else if (
haveCoordinatesChanged &&
this.props.animated &&
this.state.shape
) {
// flush current animations
(this.state.shape as AnimatedPoint).stopAnimation();
(this.state.shape as AnimatedPoint)
.timing({
coordinates: this.props.coordinates,
easing: this.props.animationEasingFunction,
duration: this.props.animationDuration,
})
.start();
}
}
onPress(event: OnPressEvent) {
if (this.props.onPress) {
this.props.onPress(event);
}
}
_getShapeFromProps(props: Partial<Props> = {}): Point {
const lng = props.coordinates?.[0] || 0;
const lat = props.coordinates?.[1] || 0;
return { type: 'Point', coordinates: [lng, lat] };
}
get symbolStyle(): SymbolLayerStyle | undefined {
if (!this.props.icon) {
return undefined;
}
return Object.assign({}, this.props.style, {
iconImage: this.props.icon,
});
}
render() {
if (!this.props.coordinates) {
return null;
}
const children = [];
if (this.symbolStyle) {
children.push(
<SymbolLayer
id={`${this.props.id}-symbol`}
style={this.symbolStyle as SymbolLayerStyle}
/>,
);
}
if (this.props.children) {
if (Array.isArray(this.props.children)) {
children.push(...this.props.children);
} else {
children.push(this.props.children);
}
}
return (
<Animated.ShapeSource
id={this.props.id}
onPress={this.onPress}
shape={this.state.shape as RNAnimated.WithAnimatedObject<Point>}
>
{children}
</Animated.ShapeSource>
);
}
}
export default Annotation;