Skip to content

Commit

Permalink
feat: 위 경도 커스터마이징 가능
Browse files Browse the repository at this point in the history
  • Loading branch information
xilucks committed Jul 21, 2024
1 parent 5e16331 commit 8d7503c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
30 changes: 14 additions & 16 deletions src/app/(playground)/playground/map/_components/KakaoMap/index.tsx
Original file line number Diff line number Diff line change
@@ -1,45 +1,43 @@
"use client";

import { useEffect, useRef } from "react";
import type { Coordinate } from "~/app/(playground)/playground/map/_store/KakaoAdressStore";
import { useKakaoAddress } from "~/app/(playground)/playground/map/_store/KakaoAdressStore";

interface KakaoMapProps {
width: string;
height: string;
level?: number;
addCenterPin?: boolean;
latitude?: number;
longitude?: number;
}

export default function KakaoMap({
width,
height,
level = 3,
addCenterPin = false,
latitude,
longitude,
}: KakaoMapProps) {
const { coordinate, setCoordinate } = useKakaoAddress();
const { coordinate } = useKakaoAddress();
const mapRef = useRef<HTMLDivElement | null>(null);

const initializeMap = () => {
if (!mapRef.current) return;

window.kakao.maps.load(() => {
const { latitude, longitude }: Coordinate = coordinate;
const center = new window.kakao.maps.LatLng(latitude, longitude);
const mapOption = {
center: center,
level: level,
};
const map = new window.kakao.maps.Map(
mapRef.current as HTMLElement,
mapOption,
const center = new window.kakao.maps.LatLng(
latitude ?? coordinate.latitude,
longitude ?? coordinate.longitude,
);
const map = new window.kakao.maps.Map(mapRef.current as HTMLElement, {
center,
level,
});

if (addCenterPin) {
const marker = new window.kakao.maps.Marker({
position: center,
});
marker.setMap(map);
new window.kakao.maps.Marker({ position: center }).setMap(map);
}
});
};
Expand All @@ -48,7 +46,7 @@ export default function KakaoMap({
if (window.kakao && window.kakao.maps) {
initializeMap();
}
}, [level, addCenterPin, coordinate]);
}, [coordinate]);

return (
<div style={{ width, height }}>
Expand Down
5 changes: 3 additions & 2 deletions src/app/(playground)/playground/map/page.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { AMain } from "~/app/(playground)/playground/inner-tools";
import AddressSearchButton from "~/app/(playground)/playground/map/_components/AddressSearchButton";
import KakaoMap from "~/app/(playground)/playground/map/_components/KakaoMap";

export default function KaKaoMapPage() {
return (
<div>
<AMain>
<div>
<KakaoMap height={"400px"} width={"100%"} addCenterPin={true} />
<AddressSearchButton />
</div>
</div>
</AMain>
);
}

0 comments on commit 8d7503c

Please sign in to comment.