-
Notifications
You must be signed in to change notification settings - Fork 50
/
index.js
199 lines (159 loc) · 4.52 KB
/
index.js
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
// @flow
import { createPortal } from 'react-dom';
import { PureComponent, createElement } from 'react';
import type MapboxMap from 'mapbox-gl/src/ui/map';
import type MapboxMarker from 'mapbox-gl/src/ui/marker';
import type LngLat from 'mapbox-gl/src/geo/lng_lat';
import type { PointLike } from '@mapbox/point-geometry';
import MapContext from '../MapContext';
import mapboxgl from '../../utils/mapbox-gl';
type Props = {
/** Marker content */
children: React$Node,
/**
* A string indicating the part of the Marker
* that should be positioned closest to the coordinate
*/
anchor:
| 'center'
| 'top'
| 'bottom'
| 'left'
| 'right'
| 'top-left'
| 'top-right'
| 'bottom-left'
| 'bottom-right',
/** The longitude of the center of the marker. */
longitude: number,
/** The latitude of the center of the marker. */
latitude: number,
/**
* The offset in pixels as a `PointLike` object to apply
* relative to the element's center. Negatives indicate left and up.
*/
offset?: PointLike,
/**
* Boolean indicating whether or not a marker is able to be dragged
* to a new position on the map.
*/
draggable?: boolean,
/**
* The rotation angle of the marker in degrees, relative to its
* respective `rotationAlignment` setting. A positive value will
* rotate the marker clockwise.
*/
rotation: number,
/**
* map aligns the `Marker` to the plane of the map. `viewport`
* aligns the Marker to the plane of the viewport. `auto` automatically
* matches the value of `rotationAlignment`.
*/
pitchAlignment: string,
/**
* map aligns the `Marker`'s rotation relative to the map, maintaining
* a bearing as the map rotates. `viewport` aligns the `Marker`'s rotation
* relative to the viewport, agnostic to map rotations.
* `auto` is equivalent to `viewport`.
*/
rotationAlignment: string,
/** Fired when the marker is clicked */
onClick?: () => any,
/** Fired when the marker is finished being dragged */
onDragEnd?: (lngLat: LngLat) => any,
/** Fired when the marker is finished being dragged */
onDragStart?: (lngLat: LngLat) => any,
/** Fired when the marker is dragged */
onDrag?: (lngLat: LngLat) => any
};
class Marker extends PureComponent<Props> {
_map: MapboxMap;
_el: HTMLDivElement;
_marker: MapboxMarker;
static displayName = 'Marker';
static defaultProps = {
anchor: 'center',
offset: null,
draggable: false,
rotation: 0,
pitchAlignment: 'auto',
rotationAlignment: 'auto'
};
constructor(props: Props) {
super(props);
this._el = document.createElement('div');
}
componentDidMount() {
const {
longitude,
latitude,
onClick,
onDragEnd,
onDragStart,
onDrag
} = this.props;
this._marker = new mapboxgl.Marker({
element: this._el,
anchor: this.props.anchor,
draggable: this.props.draggable,
offset: this.props.offset,
rotation: this.props.rotation,
pitchAlignment: this.props.pitchAlignment,
rotationAlignment: this.props.rotationAlignment
});
this._marker.setLngLat([longitude, latitude]).addTo(this._map);
if (onClick) {
this._el.addEventListener('click', onClick);
}
if (onDragEnd) {
this._marker.on('dragend', this._onDragEnd);
}
if (onDragStart) {
this._marker.on('dragstart', this._onDragStart);
}
if (onDrag) {
this._marker.on('drag', this._onDrag);
}
}
componentDidUpdate(prevProps: Props) {
const positionChanged =
prevProps.latitude !== this.props.latitude ||
prevProps.longitude !== this.props.longitude;
if (positionChanged) {
this._marker.setLngLat([this.props.longitude, this.props.latitude]);
}
}
componentWillUnmount() {
if (!this._map || !this._map.getStyle()) {
return;
}
if (this.props.onClick) {
this._el.removeEventListener('click', this.props.onClick);
}
this._marker.remove();
}
getMarker() {
return this._marker;
}
_onDragEnd = (): void => {
// $FlowFixMe
this.props.onDragEnd(this._marker.getLngLat());
};
_onDragStart = (): void => {
// $FlowFixMe
this.props.onDragStart(this._marker.getLngLat());
};
_onDrag = (): void => {
// $FlowFixMe
this.props.onDrag(this._marker.getLngLat());
};
render() {
return createElement(MapContext.Consumer, {}, (map) => {
if (map) {
this._map = map;
}
return createPortal(this.props.children, this._el);
});
}
}
export default Marker;