forked from programandopy/hackathon-desafio-ai
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMap.jsx
127 lines (118 loc) · 3.14 KB
/
Map.jsx
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
import { useCallback, useEffect, useRef, useState } from "react";
import {
AdvancedMarker,
APIProvider,
Map,
Pin,
useMap,
} from "@vis.gl/react-google-maps";
import { MarkerClusterer } from "@googlemaps/markerclusterer";
import { Circle } from "./Circle.jsx";
import POIModal from "./POIModal.jsx";
const center = {
lat: -27.33056,
lng: -55.86667,
};
const MapComponent = ({ locations = [] }) => {
return (
<div className="h-full w-full">
<APIProvider
apiKey={import.meta.env.VITE_GOOGLE_MAPS_API_KEY}
onLoad={() => console.log("Maps API has loaded.")}
>
<Map
defaultZoom={13}
defaultCenter={center}
onCameraChanged={(ev) =>
console.debug(
"camera changed:",
ev.detail.center,
"zoom:",
ev.detail.zoom
)
}
mapId="da37f3254c6a6d1c"
style={{ height: "100%", width: "100%" }}
>
<PoiMarkers locations={locations} />
</Map>
</APIProvider>
</div>
);
};
const PoiMarkers = ({ locations = [] }) => {
const map = useMap();
const [open, setOpen] = useState(false);
const [poiData, setPoiData] = useState("");
const [markers, setMarkers] = useState({});
const clusterer = useRef(null);
const [circleCenter, setCircleCenter] = useState(null);
const handleOpen = (data) => {
setPoiData(data);
setOpen(true);
};
const handleClose = () => setOpen(false);
const handleClick = useCallback((ev, poi) => {
handleOpen(poi);
console.log(ev, poi);
if (!map) return;
if (!ev.latLng) return;
map.panTo(ev.latLng);
setCircleCenter(ev.latLng);
});
// Initialize MarkerClusterer, if the map has changed
useEffect(() => {
if (!map) return;
if (!clusterer.current) {
clusterer.current = new MarkerClusterer({ map });
}
}, [map]);
// Update markers, if the markers array has changed
useEffect(() => {
clusterer.current?.clearMarkers();
clusterer.current?.addMarkers(Object.values(markers));
}, [markers]);
const setMarkerRef = (marker, key) => {
if (marker && markers[key]) return;
if (!marker && !markers[key]) return;
setMarkers((prev) => {
if (marker) {
return { ...prev, [key]: marker };
} else {
const newMarkers = { ...prev };
delete newMarkers[key];
return newMarkers;
}
});
};
return (
<>
<POIModal open={open} handleClose={handleClose} poiData={poiData} />
<Circle
radius={800}
center={circleCenter}
strokeColor={"#0c4cb3"}
strokeOpacity={1}
strokeWeight={3}
fillColor={"#3b82f6"}
fillOpacity={0.3}
/>
{locations.map((poi) => (
<AdvancedMarker
title={poi.key}
key={poi.key}
position={poi.location}
clickable={true}
onClick={(event) => handleClick(event, poi)}
>
<Pin
background={"#FBBC04"}
glyphColor={"#1a70fd"}
borderColor={"#1a70fd"}
/>
</AdvancedMarker>
))}
</>
);
};
export default MapComponent;