Skip to content

Commit 3cdbeaf

Browse files
committed
Handle infowindow close event
1 parent aa83887 commit 3cdbeaf

File tree

2 files changed

+39
-32
lines changed

2 files changed

+39
-32
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-google-maps",
3-
"version": "0.1.0",
3+
"version": "1.0.0",
44
"private": true,
55
"dependencies": {
66
"@react-google-maps/api": "^2.8.1",

src/components/map.component.jsx

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import React, { useState } from "react";
2-
import { GoogleMap, InfoWindow, LoadScript, Marker } from "@react-google-maps/api";
2+
import { GoogleMap, InfoWindow, Marker, useJsApiLoader } from "@react-google-maps/api";
33

44
const MapComponent = () => {
5+
6+
const { isLoaded, loadError } = useJsApiLoader({
7+
googleMapsApiKey: process.env.REACT_APP_GOOGLE_API_KEY
8+
});
9+
510
const initialMarkers = [
611
{
712
position: {
@@ -29,7 +34,7 @@ const MapComponent = () => {
2934
},
3035
];
3136

32-
const [activeInfoWindow, setActiveInfoWindow] = useState("");
37+
const [activeInfoWindow, setActiveInfoWindow] = useState(null);
3338
const [markers, setMarkers] = useState(initialMarkers);
3439

3540
const containerStyle = {
@@ -52,38 +57,40 @@ const MapComponent = () => {
5257
}
5358

5459
const markerDragEnd = (event, index) => {
55-
console.log(event.latLng.lat())
56-
console.log(event.latLng.lng())
60+
console.log(event.latLng.lat(), event.latLng.lng())
5761
}
5862

5963
return (
60-
<LoadScript googleMapsApiKey={process.env.REACT_APP_GOOGLE_API_KEY}>
61-
<GoogleMap
62-
mapContainerStyle={containerStyle}
63-
center={center}
64-
zoom={15}
65-
onClick={mapClicked}
66-
>
67-
{markers.map((marker, index) => (
68-
<Marker
69-
key={index}
70-
position={marker.position}
71-
label={marker.label}
72-
draggable={marker.draggable}
73-
onDragEnd={event => markerDragEnd(event, index)}
74-
onClick={event => markerClicked(marker, index)}
75-
>
76-
{
77-
(activeInfoWindow === index)
78-
&&
79-
<InfoWindow position={marker.position}>
80-
<b>{marker.position.lat}, {marker.position.lng}</b>
81-
</InfoWindow>
82-
}
83-
</Marker>
84-
))}
85-
</GoogleMap>
86-
</LoadScript>
64+
isLoaded
65+
&&
66+
<GoogleMap
67+
mapContainerStyle={containerStyle}
68+
center={center}
69+
zoom={15}
70+
onClick={mapClicked}
71+
>
72+
{markers.map((marker, index) => (
73+
<Marker
74+
key={index}
75+
position={marker.position}
76+
label={marker.label}
77+
draggable={marker.draggable}
78+
onDragEnd={event => markerDragEnd(event, index)}
79+
onClick={event => markerClicked(marker, index)}
80+
>
81+
{
82+
(activeInfoWindow === index)
83+
&&
84+
<InfoWindow
85+
position={marker.position}
86+
onCloseClick={() => setActiveInfoWindow(null)}
87+
>
88+
<b>{marker.position.lat}, {marker.position.lng}</b>
89+
</InfoWindow>
90+
}
91+
</Marker>
92+
))}
93+
</GoogleMap>
8794
);
8895
};
8996

0 commit comments

Comments
 (0)