-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuseKakaoMap.ts
More file actions
46 lines (38 loc) · 1.14 KB
/
useKakaoMap.ts
File metadata and controls
46 lines (38 loc) · 1.14 KB
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
'use client';
import { useEffect } from 'react';
import { loadKakaoSdk } from '@/shared/utils/loadKakaoSdk';
interface Location {
name: string;
lat: number;
lng: number;
}
export function useKakaoMap(
mapRef: React.RefObject<HTMLDivElement | null>,
locations: Location[],
) {
useEffect(() => {
if (!mapRef.current) return;
const initMap = () => {
if (!window.kakao) return;
window.kakao.maps.load(() => {
const map = new window.kakao.maps.Map(mapRef.current!, {
center: new window.kakao.maps.LatLng(37.498, 126.783),
level: 5,
});
locations.forEach((p) => {
const marker = new window.kakao.maps.Marker({
position: new window.kakao.maps.LatLng(p.lat, p.lng),
map,
});
const infoWindow = new window.kakao.maps.InfoWindow({
content: `<div style="padding:5px 10px; font-size:12px;">${p.name}</div>`,
});
window.kakao.maps.event.addListener(marker, 'click', () => {
infoWindow.open(map, marker);
});
});
});
};
loadKakaoSdk(initMap);
}, [mapRef, locations]);
}