forked from venits/react-native-map-clustering
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClusteredMapView.js
223 lines (201 loc) · 5.82 KB
/
ClusteredMapView.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
import React, { memo, useState, useEffect, useMemo, createRef } from "react";
import { Dimensions, LayoutAnimation, Platform } from "react-native";
import MapView, { Marker, Polyline } from "react-native-maps";
import SuperCluster from "supercluster";
import ClusterMarker from "./ClusteredMarker";
import {
isMarker,
markerToGeoJSONFeature,
calculateBBox,
returnMapZoom,
generateSpiral
} from "./helpers";
const ClusteredMapView = ({
radius,
maxZoom,
minZoom,
extent,
nodeSize,
children,
onClusterPress,
onRegionChangeComplete,
preserveClusterPressBehavior,
clusteringEnabled,
clusterColor,
clusterTextColor,
spiderLineColor,
layoutAnimationConf,
animationEnabled,
renderCluster,
...restProps
}) => {
const [markers, updateMarkers] = useState([]);
const [spiderMarkers, updateSpiderMarker] = useState([]);
const [otherChildren, updateChildren] = useState([]);
const [superCluster, setSuperCluster] = useState(null);
const [currentRegion, updateRegion] = useState(
restProps.region || restProps.initialRegion
);
const [isSpiderfier, updateSpiderfier] = useState(false);
const [clusterChildren, updateClusterChildren] = useState(null);
const mapRef = createRef();
const propsChildren = useMemo(() => React.Children.toArray(children), [
children
]);
useEffect(() => {
const rawData = [];
const otherChildren = [];
if (!clusteringEnabled) {
updateChildren(propsChildren);
return;
}
React.Children.forEach(children, (child, i) => {
if (isMarker(child)) {
rawData.push(markerToGeoJSONFeature(child, i));
} else {
otherChildren.push(child);
}
});
const superCluster = new SuperCluster({
radius,
maxZoom,
minZoom,
extent,
nodeSize
});
superCluster.load(rawData);
const bBox = calculateBBox(currentRegion);
const zoom = returnMapZoom(currentRegion, bBox, minZoom);
const markers = superCluster.getClusters(bBox, zoom);
updateMarkers(markers);
updateChildren(otherChildren);
setSuperCluster(superCluster);
}, [children, restProps.region, restProps.initialRegion]);
useEffect(() => {
if (isSpiderfier && markers.length > 0) {
let allPositions;
markers.map(marker => {
let positions = generateSpiral(
marker.properties.point_count,
marker.geometry.coordinates,
clusterChildren
);
if (allPositions instanceof Array)
allPositions = allPositions.concat(positions);
else
allPositions = positions;
})
updateSpiderMarker(allPositions);
} else {
updateSpiderMarker([]);
}
}, [isSpiderfier]);
const _onRegionChangeComplete = region => {
if (superCluster) {
const bBox = calculateBBox(region);
const zoom = returnMapZoom(region, bBox, minZoom);
const markers = superCluster.getClusters(bBox, zoom);
if (animationEnabled && Platform.OS === "ios") {
LayoutAnimation.configureNext(layoutAnimationConf);
}
if (zoom >= 17 && markers.length > 0 && clusterChildren) {
updateSpiderfier(true);
} else {
updateSpiderfier(false);
}
updateMarkers(markers);
onRegionChangeComplete(region, markers);
updateRegion(region);
}
};
const _onClusterPress = cluster => () => {
const children = superCluster.getLeaves(cluster.id, (limit = Infinity));
updateClusterChildren(children);
if (preserveClusterPressBehavior) {
onClusterPress(cluster, children);
return;
}
const coordinates = children.map(({ geometry }) => ({
latitude: geometry.coordinates[1],
longitude: geometry.coordinates[0]
}));
mapRef.current.fitToCoordinates(coordinates, {
edgePadding: restProps.edgePadding
});
onClusterPress(cluster, children);
};
return (
<MapView
{...restProps}
ref={map => {
restProps.mapRef(map);
mapRef.current = map;
}}
onRegionChangeComplete={_onRegionChangeComplete}>
{markers.map(marker =>
marker.properties.point_count === 0 ? (
propsChildren[marker.properties.index]
) : !isSpiderfier ? (
renderCluster ? (
renderCluster({
onPress: _onClusterPress(marker),
clusterColor,
clusterTextColor,
...marker
})
) : (
<ClusterMarker
key={`cluster-${marker.id}`}
{...marker}
onPress={_onClusterPress(marker)}
clusterColor={clusterColor}
clusterTextColor={clusterTextColor}
/>
)
) : null
)}
{otherChildren}
{spiderMarkers.map(marker => (
<Marker
key={marker.latitude}
coordinate={marker}
image={marker.image}
onPress={marker.onPress}
></Marker>
))}
{spiderMarkers.map((marker, index) => {
return (
<Polyline
key={index}
coordinates={[marker.centerPoint, marker, marker.centerPoint]}
strokeColor={spiderLineColor}
strokeWidth={1}
/>
)
})}
</MapView>
);
};
ClusteredMapView.defaultProps = {
clusteringEnabled: true,
animationEnabled: true,
preserveClusterPressBehavior: false,
layoutAnimationConf: LayoutAnimation.Presets.spring,
// SuperCluster parameters
radius: Dimensions.get("window").width * 0.06,
maxZoom: 20,
minZoom: 1,
extent: 512,
nodeSize: 64,
// Map parameters
edgePadding: { top: 50, left: 50, right: 50, bottom: 50 },
// Cluster styles
clusterColor: "#00B386",
clusterTextColor: "#FFFFFF",
spiderLineColor: "#FF0000",
// Callbacks
onRegionChangeComplete: () => {},
onClusterPress: () => {},
mapRef: () => {}
};
export default memo(ClusteredMapView);