-
-
Notifications
You must be signed in to change notification settings - Fork 937
Expand file tree
/
Copy pathMarkerView.tsx
More file actions
149 lines (130 loc) · 4.25 KB
/
MarkerView.tsx
File metadata and controls
149 lines (130 loc) · 4.25 KB
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
import React from 'react';
import { NativeModules, Platform, type ViewProps } from 'react-native';
import RNMBXMakerViewContentCoponent from '../specs/RNMBXMarkerViewContentNativeComponent';
import NativeMarkerViewComponent from '../specs/RNMBXMarkerViewNativeComponent';
import { type Position } from '../types/Position';
import { toJSONString } from '../utils';
import { makePoint } from '../utils/geoUtils';
import PointAnnotation from './PointAnnotation';
const Mapbox = NativeModules.RNMBXModule;
export type MarkerViewProps = ViewProps & {
/**
* The center point (specified as a map coordinate) of the marker.
*/
coordinate: Position;
/**
* Any coordinate between (0, 0) and (1, 1), where (0, 0) is the top-left corner of
* the view, and (1, 1) is the bottom-right corner. Defaults to the center at (0.5, 0.5).
*/
anchor: {
x: number;
y: number;
};
/**
* @v10
*
* Whether or not nearby markers on the map should all be displayed. If false, adjacent
* markers will 'collapse' and only one will be shown. Defaults to false.
*/
allowOverlap: boolean;
/**
* Whether or not nearby markers on the map should be hidden if close to a
* UserLocation puck. Defaults to false.
*/
allowOverlapWithPuck: boolean;
isSelected: boolean;
/**
* One or more valid React Native views.
*/
children: React.ReactElement;
};
/**
* MarkerView represents an interactive React Native marker on the map.
*
* If you have static views, consider using PointAnnotation or SymbolLayer to display
* an image, as they'll offer much better performance. Mapbox suggests using this
* component for a maximum of around 100 views displayed at one time.
*
* This is implemented with view annotations on [Android](https://docs.mapbox.com/android/maps/guides/annotations/view-annotations/)
* and [iOS](https://docs.mapbox.com/ios/maps/guides/annotations/view-annotations).
*
* This component has no dedicated `onPress` method. Instead, you should handle gestures
* with the React views passed in as `children`.
*/
class MarkerView extends React.PureComponent<MarkerViewProps> {
static defaultProps: Partial<MarkerViewProps> = {
anchor: { x: 0.5, y: 0.5 },
allowOverlap: false,
allowOverlapWithPuck: false,
isSelected: false,
};
static lastId = 0;
__idForPointAnnotation?: string;
_idForPointAnnotation(): string {
if (this.__idForPointAnnotation === undefined) {
MarkerView.lastId = MarkerView.lastId + 1;
this.__idForPointAnnotation = `MV-${MarkerView.lastId}`;
}
return this.__idForPointAnnotation;
}
_getCoordinate(coordinate: Position): string | undefined {
if (!coordinate) {
return undefined;
}
return toJSONString(makePoint(coordinate));
}
render() {
if (
this.props.anchor.x < 0 ||
this.props.anchor.y < 0 ||
this.props.anchor.x > 1 ||
this.props.anchor.y > 1
) {
console.warn(
`[MarkerView] Anchor with value (${this.props.anchor.x}, ${this.props.anchor.y}) should not be outside the range [(0, 0), (1, 1)]`,
);
}
if (Platform.OS === 'ios' && !Mapbox.MapboxV10) {
return (
<PointAnnotation id={this._idForPointAnnotation()} {...this.props} />
);
}
const { anchor = { x: 0.5, y: 0.5 } } = this.props;
return (
<RNMBXMarkerView
style={[
{
flex: 0,
alignSelf: 'flex-start',
},
this.props.style,
]}
coordinate={[
Number(this.props.coordinate[0]),
Number(this.props.coordinate[1]),
]}
anchor={anchor}
allowOverlap={this.props.allowOverlap}
allowOverlapWithPuck={this.props.allowOverlapWithPuck}
isSelected={this.props.isSelected}
onTouchEnd={(e) => {
e.stopPropagation();
}}
>
<RNMBXMakerViewContentCoponent
style={{ flex: 0, alignSelf: 'flex-start' }}
onStartShouldSetResponder={(_event) => {
return true;
}}
onTouchEnd={(e) => {
e.stopPropagation();
}}
>
{this.props.children}
</RNMBXMakerViewContentCoponent>
</RNMBXMarkerView>
);
}
}
const RNMBXMarkerView = NativeMarkerViewComponent;
export default MarkerView;