|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import React, { useState, useCallback } from "react"; |
| 4 | +import Script from "next/script"; |
| 5 | +import { Map, MapMarker, useKakaoLoader } from "react-kakao-maps-sdk"; |
| 6 | + |
| 7 | +interface Coords { |
| 8 | + lat: number; |
| 9 | + lng: number; |
| 10 | +} |
| 11 | + |
| 12 | +declare global { |
| 13 | + interface Window { |
| 14 | + daum: { |
| 15 | + Postcode: new (config: { |
| 16 | + oncomplete: (data: { address: string; zonecode: string; [key: string]: any }) => void; |
| 17 | + }) => { open: () => void }; |
| 18 | + }; |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +const KakaoMapPage = () => { |
| 23 | + const [address, setAddress] = useState(""); |
| 24 | + const [coords, setCoords] = useState<Coords>({ lat: 37.5665, lng: 126.978 }); |
| 25 | + |
| 26 | + // useKakaoLoader๋ฅผ ์ฌ์ฉํด Kakao Maps ์คํฌ๋ฆฝํธ ๋ก๋ |
| 27 | + const [loading, error] = useKakaoLoader({ |
| 28 | + appkey: process.env.NEXT_PUBLIC_KAKAO_APP_KEY as string, |
| 29 | + libraries: ["services"], // ์ถ๊ฐ ์ต์
์ผ๋ก services ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ๋ก๋ |
| 30 | + }); |
| 31 | + |
| 32 | + // ์ฃผ์ ๊ฒ์ ํจ์ (์ฃผ์ โ ์ขํ ๋ณํ) |
| 33 | + const searchAddress = useCallback(() => { |
| 34 | + if (!address) return; |
| 35 | + if (typeof window === "undefined" || !window.kakao?.maps?.services) { |
| 36 | + console.error("Kakao Maps Services not available yet"); |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + const geocoder = new window.kakao.maps.services.Geocoder(); |
| 41 | + geocoder.addressSearch(address, (result: any[], status: string) => { |
| 42 | + if (status === window.kakao.maps.services.Status.OK && result.length > 0) { |
| 43 | + const { x, y } = result[0]; // x: ๊ฒฝ๋, y: ์๋ |
| 44 | + setCoords({ lat: parseFloat(y), lng: parseFloat(x) }); |
| 45 | + } else { |
| 46 | + alert("์ฃผ์๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค. ์ ํํ ์ฃผ์๋ฅผ ์
๋ ฅํด์ฃผ์ธ์."); |
| 47 | + } |
| 48 | + }); |
| 49 | + }, [address]); |
| 50 | + |
| 51 | + // ์ฐํธ๋ฒํธ ๊ฒ์ ๋ฒํผ ํด๋ฆญ ์ ํ์
์ด๊ธฐ |
| 52 | + const handleOpenPostcode = useCallback(() => { |
| 53 | + if (typeof window === "undefined" || !window.daum) return; |
| 54 | + |
| 55 | + new window.daum.Postcode({ |
| 56 | + oncomplete: (data) => { |
| 57 | + setAddress(data.address); |
| 58 | + }, |
| 59 | + }).open(); |
| 60 | + }, []); |
| 61 | + |
| 62 | + return ( |
| 63 | + <div className="container mx-auto p-4"> |
| 64 | + {/* Daum Postcode ์คํฌ๋ฆฝํธ ๋ก๋ */} |
| 65 | + <Script src="//t1.daumcdn.net/mapjsapi/bundle/postcode/prod/postcode.v2.js" strategy="afterInteractive" /> |
| 66 | + |
| 67 | + <h1 className="mb-4 text-2xl font-bold">์ฐํธ๋ฒํธ ๊ฒ์์ผ๋ก ์ฃผ์ ์
๋ ฅ & ์ง๋ ํ์ (useKakaoLoader)</h1> |
| 68 | + <div className="mb-4 flex gap-2"> |
| 69 | + <input |
| 70 | + type="text" |
| 71 | + placeholder="์ฃผ์๋ฅผ ์
๋ ฅํ์ธ์" |
| 72 | + value={address} |
| 73 | + onChange={(e) => setAddress(e.target.value)} |
| 74 | + className="flex-1 border p-2" |
| 75 | + /> |
| 76 | + <button onClick={handleOpenPostcode} className="rounded bg-green-500 p-2 text-white hover:bg-green-600"> |
| 77 | + ์ฐํธ๋ฒํธ ์ฐพ๊ธฐ |
| 78 | + </button> |
| 79 | + <button |
| 80 | + onClick={searchAddress} |
| 81 | + disabled={loading || !!error} |
| 82 | + className="rounded bg-blue-500 p-2 text-white hover:bg-blue-600 disabled:bg-gray-400" |
| 83 | + > |
| 84 | + ๊ฒ์ |
| 85 | + </button> |
| 86 | + </div> |
| 87 | + |
| 88 | + <div className="h-96 w-full border"> |
| 89 | + {error && <div className="text-red-500">Map load error: {String(error)}</div>} |
| 90 | + {loading && !error && ( |
| 91 | + <div className="flex h-full items-center justify-center text-gray-500">Kakao Maps ๋ก๋ฉ ์ค...</div> |
| 92 | + )} |
| 93 | + {!loading && !error && ( |
| 94 | + <Map center={coords} style={{ width: "100%", height: "100%" }} level={3}> |
| 95 | + <MapMarker position={coords}>ํ์ฌ ์์น</MapMarker> |
| 96 | + </Map> |
| 97 | + )} |
| 98 | + </div> |
| 99 | + |
| 100 | + {coords && !loading && !error && ( |
| 101 | + <div className="mt-4 text-lg text-gray-700"> |
| 102 | + <p>์๋: {coords.lat}</p> |
| 103 | + <p>๊ฒฝ๋: {coords.lng}</p> |
| 104 | + </div> |
| 105 | + )} |
| 106 | + </div> |
| 107 | + ); |
| 108 | +}; |
| 109 | + |
| 110 | +export default KakaoMapPage; |
0 commit comments