UNPKG

2.78 kBJavaScriptView Raw
1import React from 'react';
2import PropTypes from 'prop-types';
3import {
4 compose,
5 defaultProps,
6 withHandlers,
7 withState,
8 withContext,
9 withProps,
10 withPropsOnChange,
11} from 'recompose';
12import { createSelector } from 'reselect';
13
14import { susolvkaCoords, generateMarkers, heatmapData } from './data/fakeData';
15
16import GoogleMapReact from '../src';
17import SimpleMarker from './markers/SimpleMarker';
18
19import ptInBounds from './utils/ptInBounds';
20import withStateSelector from './utils/withStateSelector';
21
22export const gMapHeatmap = (
23 {
24 style,
25 hoverDistance,
26 options,
27 mapParams: { center, zoom },
28 onChange,
29 onChildMouseEnter,
30 onChildMouseLeave,
31 markers,
32 draggable, // hoveredMarkerId,
33 }
34) => (
35 <GoogleMapReact
36 bootstrapURLKeys={{
37 key: 'AIzaSyBMqz4ueWMfGGqdXlvwE_cIVfar60GROi8',
38 }}
39 style={style}
40 options={options}
41 draggable={draggable}
42 hoverDistance={hoverDistance}
43 zoom={zoom}
44 center={center}
45 onChange={onChange}
46 onChildMouseEnter={onChildMouseEnter}
47 onChildMouseLeave={onChildMouseLeave}
48 heatmap={heatmapData}
49 heatmapLibrary
50 >
51 {markers}
52 </GoogleMapReact>
53);
54
55export const gMapHOC = compose(
56 defaultProps({
57 clusterRadius: 60,
58 hoverDistance: 30,
59 options: {
60 minZoom: 3,
61 maxZoom: 15,
62 },
63 style: {
64 position: 'relative',
65 margin: 10,
66 padding: 10,
67 flex: 1,
68 },
69 }),
70 withContext({ hello: PropTypes.string }, () => ({ hello: 'world' })),
71 // withState so you could change markers if you want
72 withStateSelector('markers', 'setMarkers', () =>
73 createSelector(
74 ({ route: { markersCount = 20 } }) => markersCount,
75 markersCount => generateMarkers(markersCount)
76 )),
77 withState('hoveredMarkerId', 'setHoveredMarkerId', -1),
78 withState('mapParams', 'setMapParams', { center: susolvkaCoords, zoom: 6 }),
79 // describe events
80 withHandlers({
81 onChange: ({ setMapParams }) =>
82 ({ center, zoom, bounds }) => {
83 setMapParams({ center, zoom, bounds });
84 },
85 onChildMouseEnter: ({ setHoveredMarkerId }) =>
86 (hoverKey, { id }) => {
87 setHoveredMarkerId(id);
88 },
89 onChildMouseLeave: ({ setHoveredMarkerId }) =>
90 () => {
91 setHoveredMarkerId(-1);
92 },
93 }),
94 withPropsOnChange(['markers', 'mapParams'], ({
95 markers,
96 mapParams: { bounds },
97 }) => ({
98 markers: bounds ? markers.filter(m => ptInBounds(bounds, m)) : [],
99 })),
100 withProps(({ hoveredMarkerId }) => ({
101 draggable: hoveredMarkerId === -1,
102 })),
103 withPropsOnChange(['markers'], ({ markers }) => ({
104 markers: markers.map(({ ...markerProps, id }) => (
105 <SimpleMarker key={id} id={id} {...markerProps} />
106 )),
107 }))
108);
109
110export default gMapHOC(gMapHeatmap);