Skip to content

Commit 86892ad

Browse files
committed
feat: add HeatmapLayer
1 parent ed7650f commit 86892ad

7 files changed

Lines changed: 100 additions & 4 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Google map react componentize project
1616
- [x] Circle
1717
- [ ] AdvancedMarkerView
1818
- [x] MarkerClusterer
19-
- [ ] HeatmapLayer
19+
- [x] HeatmapLayer
2020

2121
## DEMO
2222

src/components/GoogleMap/GoogleMapApiLoader.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { PropsWithChildren, useEffect } from 'react';
22

3-
import { LoadingStatus, coreStore, useCoreStore } from '../../store/core';
3+
import {
4+
LoadingStatus,
5+
coreStore,
6+
useApiLoadingStatus,
7+
useCoreStore,
8+
} from '../../store/core';
49
import { ApiLoadConfig } from '../../types';
510
import { appendLibImportScript } from '../../utils/appendScript';
611

@@ -22,7 +27,7 @@ export function GoogleMapApiLoader({
2227
}: GoogleMapApiLoaderProps) {
2328
appendLibImportScript(apiLoadConfig);
2429

25-
const status = useCoreStore((state) => state.status);
30+
const status = useApiLoadingStatus();
2631

2732
const importCoreLibrary = () =>
2833
promise ||
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import { forwardRef, useEffect, useMemo, useRef, useState } from 'react';
2+
3+
import { passRef } from '../../utils/passRef';
4+
import { useMapContext } from '../Provider/MapProvider';
5+
import { HeatmapLayerProps } from './type';
6+
import { useImportLibrary } from '../../hooks/useImportLibrary';
7+
8+
export const HeatmapLayer = forwardRef<
9+
google.maps.visualization.HeatmapLayer,
10+
HeatmapLayerProps
11+
>(function HeatmapLayer(
12+
{
13+
data,
14+
dissipating,
15+
gradient,
16+
maxIntensity,
17+
opacity,
18+
radius,
19+
hidden = false,
20+
},
21+
ref,
22+
) {
23+
const map = useMapContext();
24+
const visualizationLib = useImportLibrary('visualization');
25+
const [heatmap, setHeatmap] =
26+
useState<google.maps.visualization.HeatmapLayer | null>(null);
27+
28+
useEffect(() => {
29+
if (!visualizationLib?.HeatmapLayer) {
30+
return;
31+
}
32+
33+
const heatmap = new visualizationLib.HeatmapLayer({
34+
map,
35+
data,
36+
dissipating,
37+
gradient,
38+
maxIntensity,
39+
opacity,
40+
radius,
41+
});
42+
43+
setHeatmap(heatmap);
44+
passRef(ref, heatmap);
45+
46+
return () => {
47+
heatmap.setMap(null);
48+
};
49+
}, [visualizationLib?.HeatmapLayer]);
50+
51+
useEffect(() => {
52+
if (data) {
53+
heatmap?.setData(data);
54+
}
55+
}, [data]);
56+
57+
useEffect(() => {
58+
heatmap?.setMap(hidden ? null : map);
59+
}, [hidden]);
60+
61+
useEffect(() => {
62+
heatmap?.setOptions({
63+
gradient,
64+
});
65+
}, [gradient]);
66+
67+
useEffect(() => {
68+
heatmap?.setOptions({
69+
dissipating,
70+
maxIntensity,
71+
opacity,
72+
radius,
73+
});
74+
}, [dissipating, gradient, maxIntensity, opacity, radius]);
75+
76+
return null;
77+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { ReactNode } from 'react';
2+
3+
export interface HeatmapLayerProps
4+
extends Omit<google.maps.visualization.HeatmapLayerOptions, 'map'> {
5+
children?: ReactNode;
6+
hidden?: boolean;
7+
}

src/components/MarkerClusterer/type.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ import { MarkerClustererOptions } from '@googlemaps/markerclusterer';
22
import { ReactNode } from 'react';
33

44
export interface MarkerClustererProps
5-
extends Omit<MarkerClustererOptions, 'markers' | 'map' | ''> {
5+
extends Omit<MarkerClustererOptions, 'markers' | 'map'> {
66
children?: ReactNode;
77
}

src/components/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ export * from './Rectangle/index';
1818
export * from './Rectangle/type';
1919
export * from './Circle/index';
2020
export * from './Circle/type';
21+
export * from './HeatmapLayer/index';
22+
export * from './HeatmapLayer/type';
2123
export * from './Provider/MapProvider';
2224
export * from './Provider/MarkerProvider';
2325
export * from './Provider/CustomMarkerProvider';

src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
Polygon,
66
Rectangle,
77
Circle,
8+
HeatmapLayer,
89
CustomMarker,
910
InfoWindow,
1011
GoogleMap,
@@ -20,6 +21,7 @@ import type {
2021
PolygonProps,
2122
RectangleProps,
2223
CircleProps,
24+
HeatmapLayerProps,
2325
CustomMarkerProps,
2426
InfoWindowProps,
2527
GoogleMapProps,
@@ -41,6 +43,7 @@ export {
4143
Polygon,
4244
Rectangle,
4345
Circle,
46+
HeatmapLayer,
4447
CustomMarker,
4548
InfoWindow,
4649
GoogleMap,
@@ -61,6 +64,7 @@ export type {
6164
PolygonProps,
6265
RectangleProps,
6366
CircleProps,
67+
HeatmapLayerProps,
6468
InfoWindowProps,
6569
CustomMarkerProps,
6670
GoogleMapProps,
@@ -81,6 +85,7 @@ export default {
8185
Polygon,
8286
Rectangle,
8387
Circle,
88+
HeatmapLayer,
8489
CustomMarker,
8590
InfoWindow,
8691
GoogleMap,

0 commit comments

Comments
 (0)