All files / lib/components/GoogleMap index.tsx

0% Statements 0/21
0% Branches 0/8
0% Functions 0/6
0% Lines 0/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                                                                                                                                                           
import { GOOGLE_MAP_API_KEY } from '@lib/config';
import { GoogleMap, MarkerF, useJsApiLoader } from '@react-google-maps/api';
import { Input } from 'antd';
import React from 'react';
import { styled } from 'styled-components';
 
interface GoogleMapComponentProps {
  initialCenter?: google.maps.LatLngLiteral;
  onCenterChange?: (center: google.maps.LatLngLiteral) => void;
}
 
export default function GoogleMapComponent({
  initialCenter,
  onCenterChange
}: GoogleMapComponentProps) {
  const [currentCenter, setCurrentCenter] = React.useState<google.maps.LatLngLiteral>(
    initialCenter || {
      // 구청사 위치
      lat: 37.273958852598,
      lng: 127.00836609616198
    }
  );
 
  const [map, setMap] = React.useState<google.maps.Map | null>(null);
 
  const handleLatLngChange = (event: React.ChangeEvent<HTMLInputElement>) => {
    const { name, value } = event.target;
 
    setCurrentCenter((prev) => ({ ...prev, [name]: Number(value) }));
  };
 
  const options = {
    zoom: 16,
    mapTypeId: 'roadmap'
  };
 
  const { isLoaded } = useJsApiLoader({
    id: 'google-map-script',
    googleMapsApiKey: GOOGLE_MAP_API_KEY
  });
 
  const onLoad = React.useCallback((map: google.maps.Map) => {
    map.setCenter(currentCenter);
    map.setOptions(options);
    map.setHeading(90);
 
    setMap(map);
  }, []);
 
  React.useEffect(() => {
    map?.setCenter(currentCenter);
    onCenterChange?.(currentCenter);
  }, [currentCenter]);
 
  return (
    isLoaded && (
      <Container>
        <GoogleMapContainer>
          <GoogleMap
            id="google-map-test"
            mapContainerStyle={{ width: '100%', height: '100%' }}
            center={currentCenter}
            onLoad={onLoad}
            options={{ disableDefaultUI: true }}
            onClick={(event) => {
              setCurrentCenter({
                lat: event.latLng?.lat() || 37.273958852598,
                lng: event.latLng?.lng() || 127.00836609616198
              });
            }}
          >
            <MarkerF position={currentCenter} title="지정할 위치" />
          </GoogleMap>
        </GoogleMapContainer>
        <InputContainer>
          <div>
            <div>위도</div>
            <Input
              name="lat"
              type="number"
              value={currentCenter.lat}
              onChange={handleLatLngChange}
            />
          </div>
          <div>
            <div>경도</div>
            <Input
              name="lng"
              type="number"
              value={currentCenter.lng}
              onChange={handleLatLngChange}
            />
          </div>
        </InputContainer>
      </Container>
    )
  );
}
 
const Container = styled.div`
  display: grid;
  grid-auto-flow: column;
  gap: 8px;
`;
 
const GoogleMapContainer = styled.div`
  position: relative;
  width: 300px;
  height: 300px;
`;
 
const InputContainer = styled.div`
  input[type='number']::-webkit-inner-spin-button,
  input[type='number']::-webkit-outer-spin-button {
    display: none;
  }
`;