Skip to content

Commit a81365a

Browse files
authored
Update README.md
1 parent b08ce5e commit a81365a

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

README.md

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,114 @@ based on [google-map-react](https://github.com/istarkov/google-map-react) (witho
66
### [Demo](https://tim152.github.io/clustering-google-map-react/)
77
<img src="https://github.com/Tim152/clustering-google-map-react/blob/master/public/demo.png" alt="clustering google map react" width="410">
88

9+
```javascript
10+
import React from 'react';
11+
import GoogleMapReact from 'google-map-react';
12+
import supercluster from 'points-cluster';
13+
14+
import Marker from '../Marker';
15+
import ClusterMarker from '../ClusterMarker';
16+
17+
import mapStyles from './mapStyles.json';
18+
import { markersData, susolvkaCoords } from '../../fakeData';
19+
20+
import MapWrapper from './MapWrapper';
21+
22+
const MAP = {
23+
defaultZoom: 8,
24+
defaultCenter: susolvkaCoords,
25+
options: {
26+
styles: mapStyles,
27+
maxZoom: 19,
28+
},
29+
};
30+
31+
export class GoogleMap extends React.PureComponent {
32+
// eslint-disable-line react/prefer-stateless-function
33+
state = {
34+
mapOptions: {
35+
center: MAP.defaultCenter,
36+
zoom: MAP.defaultZoom,
37+
},
38+
clusters: [],
39+
};
40+
41+
getClusters = props => {
42+
const clusters = supercluster(markersData, {
43+
minZoom: 0,
44+
maxZoom: 16,
45+
radius: 60,
46+
});
47+
48+
return clusters(this.state.mapOptions);
49+
};
50+
51+
createClusters = props => {
52+
this.setState({
53+
clusters: this.state.mapOptions.bounds
54+
? this.getClusters(props).map(({ wx, wy, numPoints, points }) => ({
55+
lat: wy,
56+
lng: wx,
57+
numPoints,
58+
id: `${numPoints}_${points[0].id}`,
59+
points,
60+
}))
61+
: [],
62+
});
63+
};
64+
65+
handleMapChange = ({ center, zoom, bounds }) => {
66+
this.setState(
67+
{
68+
mapOptions: {
69+
center,
70+
zoom,
71+
bounds,
72+
},
73+
},
74+
() => {
75+
this.createClusters(this.props);
76+
}
77+
);
78+
};
79+
80+
render() {
81+
return (
82+
<MapWrapper>
83+
<GoogleMapReact
84+
defaultZoom={MAP.defaultZoom}
85+
defaultCenter={MAP.defaultCenter}
86+
options={MAP.options}
87+
onChange={this.handleMapChange}
88+
yesIWantToUseGoogleMapApiInternals
89+
bootstrapURLKeys={{ key: 'AIzaSyAS3ix4rVY4A-T4yPzWlEi766ycl2mY818' }}
90+
>
91+
{this.state.clusters.map(item => {
92+
if (item.numPoints === 1) {
93+
return (
94+
<Marker
95+
key={item.id}
96+
lat={item.points[0].lat}
97+
lng={item.points[0].lng}
98+
/>
99+
);
100+
}
101+
102+
return (
103+
<ClusterMarker
104+
key={item.id}
105+
lat={item.lat}
106+
lng={item.lng}
107+
points={item.points}
108+
/>
109+
);
110+
})}
111+
</GoogleMapReact>
112+
</MapWrapper>
113+
);
114+
}
115+
}
116+
117+
export default GoogleMap;
118+
```
9119
This project was bootstrapped with [Create React App](https://github.com/facebookincubator/create-react-app).

0 commit comments

Comments
 (0)