import React, { useEffect, useRef } from 'react'; import { Location } from '../models/Location'; interface MapProps { locations: Location[]; mapCenter: Location; } const Map: React.FC = ({ mapCenter, locations }) => { const mapEle = useRef(null); const map = useRef(); useEffect(() => { map.current = new google.maps.Map(mapEle.current, { center: { lat: mapCenter.lat, lng: mapCenter.lng, }, zoom: 16, }); addMarkers(); google.maps.event.addListenerOnce(map.current, 'idle', () => { if (mapEle.current) { mapEle.current.classList.add('show-map'); } }); function addMarkers() { locations.forEach(markerData => { let infoWindow = new google.maps.InfoWindow({ content: `
${markerData.name}
`, }); let marker = new google.maps.Marker({ position: new google.maps.LatLng(markerData.lat, markerData.lng), map: map.current!, title: markerData.name, }); marker.addListener('click', () => { infoWindow.open(map.current!, marker); }); }); } }, [mapCenter, locations]); return
; }; export default Map;