Skip to content

Commit 067c70d

Browse files
authored
Merge pull request tomekvenits#125 from venits/spider
Adding Spiderfier support
2 parents 6e10a96 + 729ce52 commit 067c70d

File tree

5 files changed

+34
-24
lines changed

5 files changed

+34
-24
lines changed

README.md

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ This repo is proudly sponsored by:
1313

1414
## Demo
1515

16-
![Demo](https://raw.githubusercontent.com/venits/react-native-map-clustering/master/assets/demo.gif)
16+
![Demo](https://raw.githubusercontent.com/venits/react-native-map-clustering/assets/assets/demo.gif)
17+
18+
## Spiral
19+
20+
### Converting same locations in spiral view (done automatically)
21+
22+
![Spiral](https://raw.githubusercontent.com/venits/react-native-map-clustering/assets/assets/spider_lib.png)
1723

1824
## Spiral
1925
### Converting same locations in spiral view
@@ -48,20 +54,19 @@ const INITIAL_REGION = {
4854

4955
const App = () => (
5056
<MapView initialRegion={INITIAL_REGION} style={{ flex: 1 }}>
51-
<Marker coordinate={{ latitude: 52.0, longitude: 18.2 }} />
5257
<Marker coordinate={{ latitude: 52.4, longitude: 18.7 }} />
5358
<Marker coordinate={{ latitude: 52.1, longitude: 18.4 }} />
5459
<Marker coordinate={{ latitude: 52.6, longitude: 18.3 }} />
5560
<Marker coordinate={{ latitude: 51.6, longitude: 18.0 }} />
5661
<Marker coordinate={{ latitude: 53.1, longitude: 18.8 }} />
5762
<Marker coordinate={{ latitude: 52.9, longitude: 19.4 }} />
5863
<Marker coordinate={{ latitude: 52.2, longitude: 21 }} />
59-
60-
<Marker coordinate={{ latitude: 52.8, longitude: 22 }} />
61-
<Marker coordinate={{ latitude: 52.8, longitude: 22 }} />
62-
<Marker coordinate={{ latitude: 52.8, longitude: 22 }} />
63-
<Marker coordinate={{ latitude: 52.8, longitude: 22 }} />
64-
<Marker coordinate={{ latitude: 52.8, longitude: 22 }} />
64+
<Marker coordinate={{ latitude: 52.4, longitude: 21 }} />
65+
<Marker coordinate={{ latitude: 51.8, longitude: 20 }} />
66+
<Marker coordinate={{ latitude: 51.8, longitude: 20 }} />
67+
<Marker coordinate={{ latitude: 51.8, longitude: 20 }} />
68+
<Marker coordinate={{ latitude: 51.8, longitude: 20 }} />
69+
<Marker coordinate={{ latitude: 51.8, longitude: 20 }} />
6570
</MapView>
6671
);
6772

assets/demo.gif

-2.54 MB
Binary file not shown.

lib/ClusteredMapView.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { memo, useState, useEffect, useMemo, createRef } from "react";
22
import { Dimensions, LayoutAnimation, Platform } from "react-native";
3-
import MapView, {Marker, Polyline} from 'react-native-maps';
3+
import MapView, { Marker, Polyline } from "react-native-maps";
44
import SuperCluster from "supercluster";
55
import ClusterMarker from "./ClusteredMarker";
66
import {
@@ -37,6 +37,7 @@ const ClusteredMapView = ({
3737
const [currentRegion, updateRegion] = useState(
3838
restProps.region || restProps.initialRegion
3939
);
40+
4041
const [isSpiderfier, updateSpiderfier] = useState(false);
4142
const [spiderfierMarker, updateSpiderfierMarker] = useState(null);
4243
const [clusterChildren, updateClusterChildren] = useState(null);
@@ -87,12 +88,12 @@ const ClusteredMapView = ({
8788
let positions = generateSpiral(
8889
markers[0].properties.point_count,
8990
markers[0].geometry.coordinates,
90-
clusterChildren,
91+
clusterChildren
9192
);
9293
updateSpiderMarker(positions);
9394
updateSpiderfierMarker({
9495
latitude: markers[0].geometry.coordinates[1],
95-
longitude: markers[0].geometry.coordinates[0],
96+
longitude: markers[0].geometry.coordinates[0]
9697
});
9798
} else {
9899
updateSpiderMarker([]);
@@ -108,11 +109,12 @@ const ClusteredMapView = ({
108109
if (animationEnabled && Platform.OS === "ios") {
109110
LayoutAnimation.configureNext(layoutAnimationConf);
110111
}
111-
if (zoom >= 17 && markers.length === 1) {
112+
if (zoom >= 17 && markers.length === 1 && clusterChildren) {
112113
updateSpiderfier(true);
113114
} else {
114115
updateSpiderfier(false);
115116
}
117+
116118
updateMarkers(markers);
117119
onRegionChangeComplete(region, markers);
118120
updateRegion(region);
@@ -157,7 +159,7 @@ const ClusteredMapView = ({
157159
onPress: _onClusterPress(marker),
158160
clusterColor,
159161
clusterTextColor,
160-
...marker,
162+
...marker
161163
})
162164
) : (
163165
<ClusterMarker
@@ -168,15 +170,16 @@ const ClusteredMapView = ({
168170
clusterTextColor={clusterTextColor}
169171
/>
170172
)
171-
) : null,
173+
) : null
172174
)}
173175
{otherChildren}
174176
{spiderMarkers.map(marker => (
175177
<Marker
176178
key={marker.latitude}
177179
coordinate={marker}
178180
image={marker.image}
179-
onPress={marker.onPress}></Marker>
181+
onPress={marker.onPress}
182+
></Marker>
180183
))}
181184
{spiderMarkers.map((marker, index) => {
182185
{
@@ -210,9 +213,9 @@ ClusteredMapView.defaultProps = {
210213
// Map parameters
211214
edgePadding: { top: 50, left: 50, right: 50, bottom: 50 },
212215
// Cluster styles
213-
clusterColor: '#00B386',
214-
clusterTextColor: '#FFFFFF',
215-
spiderLineColor: '#FF0000',
216+
clusterColor: "#00B386",
217+
clusterTextColor: "#FFFFFF",
218+
spiderLineColor: "#FF0000",
216219
// Callbacks
217220
onRegionChangeComplete: () => {},
218221
onClusterPress: () => {},

lib/helpers.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,19 @@ export const generateSpiral = (count, centerLocation, clusterChildren) => {
5353
let res = [];
5454
res.length = count;
5555
let angle = 0;
56+
5657
for (let i = 0; i < count; i++) {
57-
angle = 0.1 * (i * 0.5);
58-
let latitude = centerLocation[1] + 0.00025 * angle * Math.cos(angle);
59-
let longitude = centerLocation[0] + 0.00025 * angle * Math.sin(angle);
58+
angle = 0.25 * (i * 0.5);
59+
let latitude = centerLocation[1] + 0.0002 * angle * Math.cos(angle);
60+
let longitude = centerLocation[0] + 0.0002 * angle * Math.sin(angle);
6061
res[i] = {
6162
longitude,
6263
latitude,
63-
image: clusterChildren[i].properties.image,
64-
onPress: clusterChildren[i].properties.onPress,
64+
image: clusterChildren[i] && clusterChildren[i].properties.image,
65+
onPress: clusterChildren[i] && clusterChildren[i].properties.onPress
6566
};
6667
}
68+
6769
return res;
6870
};
6971

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-map-clustering",
3-
"version": "3.0.6",
3+
"version": "3.1.2",
44
"description": "Map clustering both for Android and iOS",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)