일단 로그인 후 이용 신청하기를 해서 Application 등록을 하고 Client ID 를 발급 받아야한다.
// frontend/public/index.html
<!DOCTYPE html>
<html lang="en">
...
<script type="text/javascript" src="https://oapi.map.naver.com/openapi/v3/maps.js?ncpClientId=%REACT_APP_NAVER_CLIENT_ID%"></script>
<title>React App</title>
</head>
<body>
...
</body>
</html>
// frontend/src/pages/home/home.tsx
import React, { useEffect } from 'react';
export const Home = () => {
useEffect(() => {
// Naver 지도 초기화
const map = new window.naver.maps.Map('map', {
center: new window.naver.maps.LatLng(37.5665, 126.978), // 서울의 좌표
zoom: 10, // 줌 레벨
});
// 지도에 마커 추가 예시
const marker = new window.naver.maps.Marker({
position: new window.naver.maps.LatLng(37.5665, 126.978),
map: map,
});
}, []);
return (
<>
<div id="map" style={{ width: '100%', height: '50%' }}></div>
</>
);
};
// frontend/global.d.ts
declare global {
interface Window {
naver: any;
}
}
export {};
// frontend/tsconfig.json
{
...
"include": [
"src",
"global.d.ts"
]
}
naver-maps 호출 완료
@types/navermaps
를 설치하셨으면global.d.ts
에 window가 아니라tsconfig에
"types": ["navermaps"]
를 넣고 new naver로 사용하시면 타입적용 제대로됩니다.저렇게 사용하면 new window.naver 하면 타입추론 다 any로 되서 @types/navermaps 받은 의미가 없어질텐데