Directions.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import React, { useState, useEffect, useRef } from 'react';
  2. interface DirectionsProps {
  3. disableTitle?: boolean;
  4. }
  5. const tabs = [
  6. { id: 'busan', label: '부산 본사' },
  7. { id: 'gimhae', label: '김해 공장' },
  8. { id: 'seoul', label: '서울 경기지사' },
  9. ];
  10. const tabContent: Record<string, { lat: number; lng: number; address: string; phone: string }> = {
  11. busan: {
  12. lat: 35.170833,
  13. lng: 129.130833,
  14. address: '부산 해운대구 센텀중앙로 78, 1308호(센텀그린타워)',
  15. phone: '051-808-8556',
  16. },
  17. gimhae: {
  18. lat: 35.207,
  19. lng: 128.848,
  20. address: '경남 김해시 주촌면 골든루트로 80-16',
  21. phone: '055-123-4567',
  22. },
  23. seoul: {
  24. lat: 37.501274,
  25. lng: 127.039585,
  26. address: '서울 강남구 테헤란로 123, 5층',
  27. phone: '02-987-6543',
  28. },
  29. };
  30. const Directions: React.FC<DirectionsProps> = ({ disableTitle }) => {
  31. const [activeTab, setActiveTab] = useState('busan');
  32. const mapRef = useRef<HTMLDivElement>(null);
  33. const mapInstanceRef = useRef<naver.maps.Map>(null);
  34. const markerRef = useRef<naver.maps.Marker>(null);
  35. useEffect(() => {
  36. const { naver } = window;
  37. if (!naver || !mapRef.current) return;
  38. const location = tabContent[activeTab];
  39. const position = new naver.maps.LatLng(location.lat, location.lng);
  40. if (!mapInstanceRef.current) {
  41. mapInstanceRef.current = new naver.maps.Map(mapRef.current, {
  42. center: position,
  43. zoom: 16,
  44. });
  45. } else {
  46. mapInstanceRef.current.setCenter(position);
  47. }
  48. // ✅ 기존 마커 제거 후 새로운 마커 설정
  49. if (markerRef.current) {
  50. markerRef.current.setMap(null);
  51. }
  52. markerRef.current = new naver.maps.Marker({
  53. position,
  54. map: mapInstanceRef.current,
  55. });
  56. }, [activeTab]);
  57. return (
  58. <div className="px-[2vw] pb-[4vw] space-y-[5vw]">
  59. <div className="max-w-7xl mx-auto">
  60. {!disableTitle && (
  61. <div className="space-y-2">
  62. <div className="text-4xl text-blue-900 font-bold font-['Inter']">찾아오시는 길</div>
  63. </div>
  64. )}
  65. <div className="mx-30 h-40 flex text-center items-center justify-around">
  66. {tabs.map((tab, idx) => (
  67. <div
  68. key={tab.id}
  69. className={`w-full py-6 cursor-pointer text-2xl font-semibold font-['Inter'] ${
  70. idx !== 0 ? 'border-l' : ''
  71. } ${idx !== tabs.length - 1 ? 'border-r' : ''} ${
  72. activeTab === tab.id ? 'text-black' : 'text-black/25'
  73. }`}
  74. style={{ borderColor: '#E0E1E2' }}
  75. onClick={() => setActiveTab(tab.id)}
  76. >
  77. {tab.label}
  78. </div>
  79. ))}
  80. </div>
  81. <div className="mx-30">
  82. <div style={{ height: '500px' }}>
  83. <div id="map" ref={mapRef} style={{ width: '100%', height: '100%' }} />
  84. </div>
  85. <div className="flex flex-wrap py-6">
  86. <div className="w-1/2 px-4">
  87. <div className="text-black text-2xl font-semibold font-['Inter'] mb-2">주소</div>
  88. <div className="text-black text-xl font-normal font-['Inter']">
  89. {tabContent[activeTab].address}
  90. </div>
  91. </div>
  92. <div className="w-1/2 px-4">
  93. <div className="text-black text-2xl font-semibold font-['Inter'] mb-2">전화</div>
  94. <div className="text-black text-xl font-normal font-['Inter']">
  95. {tabContent[activeTab].phone}
  96. </div>
  97. </div>
  98. </div>
  99. </div>
  100. </div>
  101. </div>
  102. );
  103. };
  104. export default Directions;