-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathclusters.js
198 lines (181 loc) · 4.91 KB
/
clusters.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
// @flow
/**
* <!-- {"order": 7} -->
*
* # Clusters example
*
* Real life example. _(clusters, zoom on marker click)_
*
*/
import * as React from 'react';
import { Map, Overlay, Marker } from 'rgm';
import { css } from '@emotion/core';
// $FlowFixMe
import Supercluster from 'supercluster';
import { useGoogleApiLoader } from '../dev-src/hooks';
import { Ratio } from '../dev-src/controls';
import { getScreenOffset } from '../dev-src/geo-utils';
// $FlowFixMe
import places from '../data/places.json';
import type { StaticProps } from '../dev-src/doc.js';
const superclusterIndex = new Supercluster({
log: false,
radius: 60,
extent: 256,
maxZoom: 17,
}).load(places.features);
// https://developers.google.com/maps/documentation/javascript/reference/map#MapOptions
const MAP_OPTIONS = {
zoom: 1,
maxZoom: 17,
center: {
lat: 0,
lng: 30.304,
},
// disable cmd-zoom and 2 fingers zoom I personally dislike it
gestureHandling: 'greedy',
clickableIcons: false,
};
type Cluster = {|
geometry: {|
coordinates: [number, number],
|},
id?: number,
properties: {|
cluster?: boolean,
cluster_id?: number,
point_count?: number,
|},
|};
// Google has no "zoom at point" method
const zoomAt = (map, pt, zoom) => {
const center = map.getCenter();
const centerLatLng = {
lat: center.lat(),
lng: center.lng(),
};
const offsetA = getScreenOffset(pt, centerLatLng, map.getZoom());
const offsetB = getScreenOffset(pt, centerLatLng, zoom);
const x = offsetA.x - offsetB.x;
const y = offsetA.y - offsetB.y;
map.setZoom(zoom);
map.panBy(x, y);
};
export default function Clusters(): React.Node {
const api = useGoogleApiLoader();
const [map, setMap] = React.useState(null);
const [clusters, setClusters] = React.useState<$ReadOnlyArray<Cluster>>([]);
React.useEffect(() => {
if (map != null) {
const boundsChangedListener = map.addListener('idle', () => {
const bounds = map.getBounds();
const zoom = map.getZoom();
const sw = bounds.getSouthWest();
const ne = bounds.getNorthEast();
const swA = [sw.lng(), sw.lat()];
const neA = [ne.lng(), ne.lat()];
// Supercluster don't work in some cases, fix it
if (swA[0] > neA[0] && swA[0] - neA[0] < 0.00001) {
swA[0] = -180;
neA[0] = 180;
}
const clusters = superclusterIndex.getClusters([...swA, ...neA], zoom);
setClusters(clusters);
});
return () => {
boundsChangedListener.remove();
};
}
}, [map]);
return (
<Ratio value={3 / 4}>
{api && (
<Map ref={setMap} api={api} options={MAP_OPTIONS}>
<Overlay debug={false}>
{clusters.map(cluster => {
const [lng, lat] = cluster.geometry.coordinates;
return (
<Marker key={`${lng} - ${lat}`} lng={lng} lat={lat}>
<ClusterMarker
count={cluster.properties.point_count ?? null}
onClick={() => {
if (map && cluster.id != null) {
zoomAt(
map,
{
lng,
lat,
},
superclusterIndex.getClusterExpansionZoom(cluster.id),
);
}
}}
/>
</Marker>
);
})}
</Overlay>
</Map>
)}
</Ratio>
);
}
// css is awesome!
const ClusterMarker = ({ count, onClick }) => {
const text = count ?? '';
return (
<div
onClick={onClick}
css={css`
position: relative;
place-self: center center;
border-radius: 100%;
border: 3px solid #eee;
background-color: white;
padding: ${count == null ? 2 : 8}px;
box-shadow: 0 0 0 2px #fe4a0d, 0 0 0 4px white;
&:after {
padding-top: 100%;
content: ' ';
display: block;
}
font-size: 1rem;
&:hover {
padding: ${count == null ? 3 : 10}px;
transition: padding 0.07s ease-out;
z-index: 1;
}
transition: padding 0.07s ease-in;
cursor: pointer;
`}
>
<div
css={css`
height: 0;
overflow: hidden;
`}
>
{text}
</div>
<div
css={css`
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
`}
>
{text}
</div>
</div>
);
};
export const getStaticProps = async (): Promise<StaticProps> => {
// The best is to place this method at _app.js but this doesn't work now
const doc = await import('../dev-src/doc');
return doc.getStaticProps();
};