forked from react-native-maps/react-native-maps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMapMarker.js
287 lines (249 loc) · 7.32 KB
/
MapMarker.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
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
import React, { PropTypes } from 'react';
import {
View,
StyleSheet,
Platform,
NativeModules,
Animated,
findNodeHandle,
} from 'react-native';
import resolveAssetSource from 'react-native/Libraries/Image/resolveAssetSource';
import decorateMapComponent, {
SUPPORTED,
USES_DEFAULT_IMPLEMENTATION,
} from './decorateMapComponent';
const viewConfig = {
uiViewClassName: 'AIR<provider>MapMarker',
validAttributes: {
coordinate: true,
},
};
const propTypes = {
...View.propTypes,
// TODO(lmr): get rid of these?
identifier: PropTypes.string,
reuseIdentifier: PropTypes.string,
/**
* The title of the marker. This is only used if the <Marker /> component has no children that
* are an `<MapView.Callout />`, in which case the default callout behavior will be used, which
* will show both the `title` and the `description`, if provided.
*/
title: PropTypes.string,
/**
* The description of the marker. This is only used if the <Marker /> component has no children
* that are an `<MapView.Callout />`, in which case the default callout behavior will be used,
* which will show both the `title` and the `description`, if provided.
*/
description: PropTypes.string,
/**
* A custom image to be used as the marker's icon. Only local image resources are allowed to be
* used.
*/
image: PropTypes.any,
/**
* 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.
*/
pinColor: PropTypes.string,
/**
* The coordinate for the marker.
*/
coordinate: PropTypes.shape({
/**
* Coordinates for the anchor point of the marker.
*/
latitude: PropTypes.number.isRequired,
longitude: PropTypes.number.isRequired,
}).isRequired,
/**
* The offset (in points) at which to display the view.
*
* By default, the center point of an annotation view is placed at the coordinate point of the
* associated annotation. You can use this property to reposition the annotation view as
* needed. This x and y offset values are measured in points. Positive offset values move the
* annotation view down and to the right, while negative values move it up and to the left.
*
* For android, see the `anchor` prop.
*
* @platform ios
*/
centerOffset: PropTypes.shape({
/**
* Offset from the anchor point
*/
x: PropTypes.number.isRequired,
y: PropTypes.number.isRequired,
}),
/**
* The offset (in points) at which to place the callout bubble.
*
* This property determines the additional distance by which to move 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.
*
* For android, see the `calloutAnchor` prop.
*
* @platform ios
*/
calloutOffset: PropTypes.shape({
/**
* Offset to the callout
*/
x: PropTypes.number.isRequired,
y: PropTypes.number.isRequired,
}),
/**
* 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).
*
* For ios, see the `centerOffset` prop.
*
* @platform android
*/
anchor: PropTypes.shape({
/**
* Offset to the callout
*/
x: PropTypes.number.isRequired,
y: PropTypes.number.isRequired,
}),
/**
* 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 `andor` prop for more
* details.
*
* The default is the top middle of the image.
*
* For ios, see the `calloutOffset` prop.
*
* @platform android
*/
calloutAnchor: PropTypes.shape({
/**
* Offset to the callout
*/
x: PropTypes.number.isRequired,
y: PropTypes.number.isRequired,
}),
/**
* Sets whether this marker should be flat against the map true or a billboard facing the
* camera false.
*
* @platform android
*/
flat: PropTypes.bool,
draggable: PropTypes.bool,
/**
* Callback that is called when the user presses on the marker
*/
onPress: PropTypes.func,
/**
* Callback that is called when the user selects the marker, before the callout is shown.
*
* @platform ios
*/
onSelect: PropTypes.func,
/**
* Callback that is called when the marker is deselected, before the callout is hidden.
*
* @platform ios
*/
onDeselect: PropTypes.func,
/**
* Callback that is called when the user taps the callout view.
*/
onCalloutPress: PropTypes.func,
/**
* Callback that is called when the user initiates a drag on this marker (if it is draggable)
*/
onDragStart: PropTypes.func,
/**
* Callback called continuously as the marker is dragged
*/
onDrag: PropTypes.func,
/**
* Callback that is called when a drag on this marker finishes. This is usually the point you
* will want to setState on the marker's coordinate again
*/
onDragEnd: PropTypes.func,
};
const defaultProps = {
onPress() {},
};
class MapMarker extends React.Component {
constructor(props) {
super(props);
this.showCallout = this.showCallout.bind(this);
this.hideCallout = this.hideCallout.bind(this);
}
showCallout() {
this._runCommand('showCallout', []);
}
hideCallout() {
this._runCommand('hideCallout', []);
}
_getHandle() {
return findNodeHandle(this.marker);
}
_runCommand(name, args) {
switch (Platform.OS) {
case 'android':
NativeModules.UIManager.dispatchViewManagerCommand(
this._getHandle(),
this.getUIManagerCommand(name),
args
);
break;
case 'ios':
this.getMapManagerCommand(name)(this._getHandle(), ...args);
break;
default:
break;
}
}
render() {
let image;
if (this.props.image) {
image = resolveAssetSource(this.props.image) || {};
image = image.uri;
}
const AIRMapMarker = this.getAirComponent();
return (
<AIRMapMarker
ref={ref => { this.marker = ref; }}
{...this.props}
image={image}
style={[styles.marker, this.props.style]}
/>
);
}
}
MapMarker.propTypes = propTypes;
MapMarker.defaultProps = defaultProps;
MapMarker.viewConfig = viewConfig;
const styles = StyleSheet.create({
marker: {
position: 'absolute',
backgroundColor: 'transparent',
},
});
MapMarker.Animated = Animated.createAnimatedComponent(MapMarker);
module.exports = decorateMapComponent(MapMarker, {
componentType: 'Marker',
providers: {
google: {
ios: SUPPORTED,
android: USES_DEFAULT_IMPLEMENTATION,
},
},
});