KakaoMap.js 1.13 KB
Newer Older
Kim, Subin's avatar
Kim, Subin committed
1
2
3
4
import { useEffect, useRef } from "react";

const { kakao } = window;

Kim, Subin's avatar
Kim, Subin committed
5
const KakaoMap = ({ address }) => {
Kim, Subin's avatar
Kim, Subin committed
6
7
8
9
10
11
12
13
14
15
16
    const kakaoMapDiv = useRef(null)

    useEffect(() => {
        const container = kakaoMapDiv.current
        const options = {
            center: new kakao.maps.LatLng(33.450701, 126.570667),
            level: 3
        };
        const map = new kakao.maps.Map(container, options);

        const geocoder = new kakao.maps.services.Geocoder();
Kim, Subin's avatar
Kim, Subin committed
17
        geocoder.addressSearch(`${address}`, function (result, status) {
Kim, Subin's avatar
Kim, Subin committed
18
19
20
21
22
23
24
            if (status === kakao.maps.services.Status.OK) {
                const coords = new kakao.maps.LatLng(result[0].y, result[0].x);
                const marker = new kakao.maps.Marker({
                    map: map,
                    position: coords
                });
                map.setCenter(coords);
Kim, Subin's avatar
Kim, Subin committed
25
            } else if (address != '') {
Kim, Subin's avatar
Kim, Subin committed
26
27
28
                alert("찾을 수 없는 주소입니다. 다시 입력해주세요.")
            }
        });
Kim, Subin's avatar
Kim, Subin committed
29
    }, [address])
Kim, Subin's avatar
Kim, Subin committed
30
31
32
33
34
35
36

    return (
        <div ref={kakaoMapDiv} style={{ width: "500px", height: "400px" }}></div>
    )
}

export default KakaoMap