-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMapUtils.js
54 lines (44 loc) · 1.35 KB
/
MapUtils.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
import {Dimensions} from 'react-native';
import supercluster from 'supercluster';
const {width, height} = Dimensions.get('window');
const ASPECT_RATIO = width / height;
export function getRegion(latitude, longitude, latitudeDelta) {
const LONGITUDE_DELTA = latitudeDelta * ASPECT_RATIO;
return {
latitude: latitude,
longitude: longitude,
latitudeDelta: latitudeDelta,
longitudeDelta: LONGITUDE_DELTA,
};
}
export function getCluster(places, region) {
const cluster = supercluster({
radius: 50,
maxZoom: 16,
});
let markers = [];
try {
const padding = 0;
cluster.load(places);
markers = cluster.getClusters(
[
region.longitude - (region.longitudeDelta * (0.5 + padding)),
region.latitude - (region.latitudeDelta * (0.5 + padding)),
region.longitude + (region.longitudeDelta * (0.5 + padding)),
region.latitude + (region.latitudeDelta * (0.5 + padding)),
],
getZoomLevel(region.longitudeDelta)
);
}
catch (e) {
console.debug('failed to create cluster', e);
}
return {
markers,
cluster
};
}
function getZoomLevel(longitudeDelta) {
const angle = longitudeDelta;
return Math.round(Math.log(360 / angle) / Math.LN2);
}