-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[AUD-7] T Map SDK 활용을 위한 TMapModule 및 useTMap Hook 제작 #4
Changes from all commits
e7205af
666dda3
9963375
84da353
3a8bff9
b7a889a
75ad00d
52c6ea4
09e427a
6c57e05
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
VITE_TMAP_APP_KEY= |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,8 @@ dist | |
dist-ssr | ||
*.local | ||
|
||
.env | ||
|
||
# Editor directories and files | ||
.vscode/* | ||
!.vscode/extensions.json | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<link rel="icon" type="image/svg+xml" href="/vite.svg" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>여행 지도 계획 플래너, 어디 (Audy)</title> | ||
</head> | ||
<body> | ||
<div id="root"></div> | ||
<script type="module" src="/src/main.tsx"></script> | ||
</body> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<link rel="icon" type="image/svg+xml" href="/vite.svg" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>여행 지도 계획 플래너, 어디 (Audy)</title> | ||
<script src="https://apis.openapi.sk.com/tmap/vectorjs?version=1&appKey=%VITE_TMAP_APP_KEY%"></script> | ||
</head> | ||
<body> | ||
<div id="root"></div> | ||
<script type="module" src="/src/main.tsx"></script> | ||
</body> | ||
</html> |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,57 @@ | ||
import { Route, Routes } from 'react-router-dom'; | ||
import { useState } from 'react'; | ||
|
||
import LoginPage from './pages/LoginPage'; | ||
import RedirectionPage from './pages/RedirectionPage'; | ||
import reactLogo from '@/assets/react.svg'; | ||
import { useTmap } from '@/hooks/useTmap'; | ||
|
||
import viteLogo from '/vite.svg'; | ||
|
||
import './App.css'; | ||
|
||
function App() { | ||
<Routes> | ||
<Route path="/" element={<LoginPage />} /> | ||
<Route path="/redirect" element={<RedirectionPage />} /> | ||
</Routes>; | ||
const [count, setCount] = useState(0); | ||
|
||
const { tmapModuleRef, mapContainerRef } = useTmap({ | ||
mapId: 'tmap', | ||
latitude: 37.5652045, | ||
longitude: 126.98702028, | ||
}); | ||
|
||
const handleTestAddMarker = () => { | ||
if (!tmapModuleRef.current) return; | ||
console.log(tmapModuleRef.current); | ||
tmapModuleRef.current.setMarker({latitude: 37.56520450, longitude: 126.98602028}); | ||
|
||
} | ||
|
||
return <LoginPage />; | ||
return ( | ||
<> | ||
<div> | ||
<div ref={mapContainerRef} /> | ||
<a href="https://vitejs.dev" target="_blank"> | ||
<img src={viteLogo} className="logo" alt="Vite logo" /> | ||
</a> | ||
<a href="https://react.dev" target="_blank"> | ||
<img | ||
src={reactLogo} | ||
className="logo react" | ||
alt="React logo" | ||
/> | ||
</a> | ||
</div> | ||
<h1>Vite + React</h1> | ||
<div className="card"> | ||
<button onClick={handleTestAddMarker}> | ||
count is {count} | ||
</button> | ||
<p> | ||
Edit <code>src/App.tsx</code> and save to test HMR | ||
</p> | ||
</div> | ||
<p className="read-the-docs"> | ||
Click on the Vite and React logos to learn more | ||
</p> | ||
</> | ||
); | ||
} | ||
|
||
export default App; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { useEffect, useRef } from 'react'; | ||
|
||
import { TMapModule, type TmapConstructorType } from '@/utils/tmap'; | ||
|
||
export const useTmap = ({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. T Map 을 렌더링하려는 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 설명 감사합니다 ~! 좋은 방법이네요 |
||
mapId = 'tmap', | ||
width = 640, | ||
height = 480, | ||
zoom = 10, | ||
latitude, | ||
longitude, | ||
}: TmapConstructorType) => { | ||
const mapContainerRef = useRef<HTMLDivElement | null>(null); | ||
const tmapModuleRef = useRef<TMapModule | null>(null); | ||
|
||
useEffect(() => { | ||
if (!mapContainerRef.current) return; | ||
|
||
mapContainerRef.current.id = mapId; | ||
tmapModuleRef.current = new TMapModule({ | ||
mapId, | ||
width, | ||
height, | ||
zoom, | ||
latitude, | ||
longitude, | ||
}); | ||
}, [height, latitude, longitude, mapId, width, zoom]); | ||
|
||
return { mapContainerRef, tmapModuleRef }; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,6 @@ | ||
import { StrictMode } from 'react'; | ||
|
||
import { createRoot } from 'react-dom/client'; | ||
|
||
import App from './App.tsx'; | ||
import './index.css'; | ||
|
||
createRoot(document.getElementById('root')!).render( | ||
<StrictMode> | ||
<App /> | ||
</StrictMode>, | ||
); | ||
createRoot(document.getElementById('root')!).render(<App />); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
export interface TmapConstructorType { | ||
/** 지도를 렌더링할 HTMLDivElement 에 적용할 id */ | ||
mapId?: string; | ||
/** 지도 Element 의 width (px) */ | ||
width?: number; | ||
/** 지도 Element 의 height (px) */ | ||
height?: number; | ||
/** 지도 Element 의 확대 정도 (1 ~ 15) */ | ||
zoom?: number; | ||
/** 지도 의 중심점 위도 */ | ||
latitude: number; | ||
/** 지도 의 중심점 경도 */ | ||
longitude: number; | ||
Comment on lines
+11
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 다른 것들도 옵셔널이면 얘네도 그냥 기본값 줘도 되지 않을까요? 뭐 서울시청 이런식으로 해서 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 중심좌표 값이 없을 경우에는 내부적으로 어디를 기본으로 바라볼지만 얼라인 맞추면 좋을거 같습니다 |
||
} | ||
|
||
export class TMapModule { | ||
#mapInstance: typeof window.Tmapv3; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 추후 이 mapInstance 를 기반으로 TMap API 를 활용하도록 메서드를 작성해주시면 됩니다. export class TMapModule {
#mapInstance: typeof window.Tmapv3;
// 지도에 생성된 마커들을 보관하는 배열 markerList (private)
#markerList: (typeof window.Tmapv3.Marker)[] = [];
constructor({
mapId = 'tmap',
width = 640,
height = 480,
zoom = 10,
latitude,
longitude,
}: TmapConstructorType) { ... 중략 }
// Map Instance 에 새로운 마커를 추가하는 메서드 setMarker
setMarker({
latitude,
longitude,
iconUrl,
}: {
latitude: number;
longitude: number;
iconUrl: string;
}) {
const marker = new window.Tmapv3.Marker({
position: new window.Tmapv3.LatLng(latitude, longitude),
iconUrl,
map: this.#mapInstance,
});
this.#markerList = [...this.#markerList, marker];
}
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 참고하도록 하겠습니다! |
||
|
||
constructor({ | ||
mapId = 'tmap', | ||
width = 640, | ||
height = 480, | ||
zoom = 10, | ||
latitude, | ||
longitude, | ||
}: TmapConstructorType) { | ||
if (typeof window === 'undefined') { | ||
throw new Error('T Map 은 Server Side 에서 사용할 수 없습니다.'); | ||
} | ||
Comment on lines
+27
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 해당 코드가 서버 단에서 실행될 경우에는 window 변수가 별도로 Global Context 에 존재하지 않습니다. 실제로 NodeJS 에서 아래 코드를 실행하면 이런 에러가 발생합니다. if (!window) {
console.log('it is Node Rumtime')
} 하지만 if (typeof window === 'undefined') {
console.log('it is Node Rumtime')
}
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 설명 감사합니다 ! |
||
|
||
const mapElement = document.getElementById(mapId); | ||
|
||
if (!mapElement) { | ||
throw new Error( | ||
'T Map 을 렌더링하기 위해 필요한 HTMLDivElement 가 없습니다.', | ||
); | ||
} | ||
|
||
this.#mapInstance = new window.Tmapv3.Map(mapId, { | ||
center: new window.Tmapv3.LatLng(latitude, longitude), | ||
width: `${width}px`, | ||
height: `${height}px`, | ||
zoom, | ||
}); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
여기 중간에 공백있는건 import 정렬 플러그인이 자동으로 띄워놓은건가요 ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
그렇습니다. 정렬 기준은 AUD-02 PR에서 확인 가능하세요