UNPKG

3.27 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 {
15 susolvkaCoords,
16 generateMarkers,
17 heatmapData,
18 generateHeatmapData,
19} from './data/fakeData';
20
21import GoogleMapReact from '../src';
22import SimpleMarker from './markers/SimpleMarker';
23
24import ptInBounds from './utils/ptInBounds';
25import withStateSelector from './utils/withStateSelector';
26import { GOOGLE_API_KEY } from './config/Google_API_key';
27import withSafeInterval from './utils/withSafeInterval';
28
29export const gMapHeatmap = (
30 {
31 style,
32 hoverDistance,
33 options,
34 heatmap,
35 mapParams: { center, zoom },
36 onChange,
37 onChildMouseEnter,
38 onChildMouseLeave,
39 markers,
40 draggable, // hoveredMarkerId,
41 }
42) => (
43 <GoogleMapReact
44 bootstrapURLKeys={{
45 key: GOOGLE_API_KEY,
46 }}
47 style={style}
48 options={options}
49 draggable={draggable}
50 hoverDistance={hoverDistance}
51 zoom={zoom}
52 center={center}
53 onChange={onChange}
54 onChildMouseEnter={onChildMouseEnter}
55 onChildMouseLeave={onChildMouseLeave}
56 heatmap={heatmap}
57 heatmapLibrary
58 >
59 {markers}
60 </GoogleMapReact>
61);
62
63export const gMapHOC = compose(
64 defaultProps({
65 clusterRadius: 60,
66 hoverDistance: 30,
67 options: {
68 minZoom: 3,
69 maxZoom: 15,
70 },
71 style: {
72 position: 'relative',
73 margin: 10,
74 padding: 10,
75 flex: 1,
76 },
77 }),
78 withContext({ hello: PropTypes.string }, () => ({ hello: 'world' })),
79 // withState so you could change markers if you want
80 withStateSelector('markers', 'setMarkers', () =>
81 createSelector(
82 ({ route: { markersCount = 20 } }) => markersCount,
83 markersCount => generateMarkers(markersCount)
84 )),
85 withState('hoveredMarkerId', 'setHoveredMarkerId', -1),
86 withState('mapParams', 'setMapParams', { center: susolvkaCoords, zoom: 6 }),
87 withSafeInterval,
88 withState('heatmap', 'setHeatmap', heatmapData),
89 // describe events
90 withHandlers({
91 onChange: ({ setMapParams, setSafeInterval, setHeatmap, mapParams }) =>
92 ({ center, zoom, bounds }) => {
93 setMapParams({ center, zoom, bounds });
94 const boundSetHeatmap = setHeatmap.bind(this);
95 setSafeInterval(
96 () => {
97 boundSetHeatmap(
98 generateHeatmapData(mapParams.center.lat, mapParams.center.lng)
99 );
100 },
101 3000
102 );
103 },
104 onChildMouseEnter: ({ setHoveredMarkerId }) =>
105 (hoverKey, { id }) => {
106 setHoveredMarkerId(id);
107 },
108 onChildMouseLeave: ({ setHoveredMarkerId }) =>
109 () => {
110 setHoveredMarkerId(-1);
111 },
112 }),
113 withPropsOnChange(['markers', 'mapParams'], ({
114 markers,
115 mapParams: { bounds },
116 }) => ({
117 markers: bounds ? markers.filter(m => ptInBounds(bounds, m)) : [],
118 })),
119 withProps(({ hoveredMarkerId }) => ({
120 draggable: hoveredMarkerId === -1,
121 })),
122 withPropsOnChange(['markers'], ({ markers }) => ({
123 markers: markers.map(({ ...markerProps, id }) => (
124 <SimpleMarker key={id} id={id} {...markerProps} />
125 )),
126 }))
127);
128
129export default gMapHOC(gMapHeatmap);