Skip to content
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

feat: 모집 상세 페이지 지도위에 코스 나타내기 성공 #101

Merged
merged 1 commit into from
Nov 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions client/src/hooks/useShowMap.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import ZoomControl from "#components/MapControl/ZoomControl/ZoomControl";
import { LatLng } from "#types/LatLng";
import { MapProps } from "#types/MapProps";
import { useCallback, useEffect, useRef, useState } from "react";
import useZoomControl from "./useZoomControl";

const useShowMap = ({ height = "100vh", center, level = 1, runningPath }: MapProps) => {
const container = useRef<HTMLDivElement>(null);
const map = useRef<kakao.maps.Map>();
const polyLineRef = useRef<kakao.maps.Polyline>();
const [path, setPath] = useState<kakao.maps.LatLng[]>([]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

showMap에서는 path를 state로 관리할 필요가 없는 것 같아요

const { zoomIn, zoomOut } = useZoomControl(map);

useEffect(() => {
if (!container.current) return;
map.current = new kakao.maps.Map(container.current, {
center: new kakao.maps.LatLng(center.lat, center.lng),
level,
});
polyLineRef.current = new kakao.maps.Polyline({
map: map.current,
path,
});
DrawPath(runningPath);
}, [runningPath]);
const getLaMaByLatLng = (point: LatLng): kakao.maps.LatLng => {
return new kakao.maps.LatLng(point.lat, point.lng);
};
const DrawPath = useCallback(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DrawPath() -> drawPath()

async (path: { lat: number; lng: number }[] | undefined) => {
if (!path) {
return;
}
if (!polyLineRef.current) return;
for (const point of path) {
const position = getLaMaByLatLng(point);
const newPath = [...polyLineRef.current.getPath(), position];
setPath(newPath);
polyLineRef.current.setPath(newPath);
}
},
[polyLineRef],
);

return {
map: map.current,
path,
renderMap: () => (
<div style={{ position: "relative" }}>
<div ref={container} style={{ width: "100vw", height }} />
<ZoomControl onClickZoomIn={zoomIn} onClickZoomOut={zoomOut} />
</div>
),
};
};

export default useShowMap;
1 change: 1 addition & 0 deletions client/src/hooks/useWriteMap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const useWriteMap = ({ height = "100vh", center, level = 1 }: MapProps) => {
async (mouseEvent: kakao.maps.event.MouseEvent) => {
if (!polyLineRef.current) return;
const position = mouseEvent.latLng;
console.log(mouseEvent.latLng);
const isRoad = await checkIsRoad(position);
if (isRoad) {
const newPath = [...polyLineRef.current.getPath(), position];
Expand Down
39 changes: 36 additions & 3 deletions client/src/pages/RecruitDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { useRecoilValue } from "recoil";
import useHttpPost from "#hooks/http/useHttpPost";
import useHttpGet from "#hooks/http/useHttpGet";
import { useEffect, useState, useCallback } from "react";
import ViewMap from "#components/Map/ViewMap/ViewMap";
import useShowMap from "#hooks/useShowMap";

const RecruitDetail = () => {
const { id } = useParams();
Expand All @@ -21,6 +21,8 @@ const RecruitDetail = () => {
const [author, setAuthor] = useState("게시자");
const [maxPpl, setMaxPpl] = useState("최대 인원");
const [currentPpl, setCurrentPpl] = useState("현재 인원");
const [path, setPath] = useState([]);
const [middlePoint, setMiddlePoint] = useState({ lat: 0, lng: 0 });

const getPaceFormat = (sec: number): string => {
return `${parseInt(String(sec / 60))}'${sec % 60}"`;
Expand All @@ -44,7 +46,36 @@ const RecruitDetail = () => {
alert(error.message);
}
};

const renderMap = useCallback(
useShowMap({
height: `${window.innerHeight - 307}px`,
center: { lat: middlePoint.lat, lng: middlePoint.lng },
runningPath: path,
level: 5,
}).renderMap,
[path, middlePoint],
);
const getMiddlePoint = (path: { lat: number; lng: number }[]) => {
let minLat = 90;
let maxLat = -90;
let minLng = 180;
let maxLng = -180;
for (const point of path) {
if (minLat > point.lat) {
minLat = point.lat;
}
if (maxLat < point.lat) {
maxLat = point.lat;
}
if (minLng > point.lng) {
minLng = point.lng;
}
if (maxLng < point.lng) {
maxLng = point.lng;
}
}
return { lat: (minLat + maxLat) / 2, lng: (minLng + maxLng) / 2 };
};
const getRecruitDetail = useCallback(async () => {
try {
const response = await get(`/recruit/${id}`);
Expand All @@ -55,6 +86,8 @@ const RecruitDetail = () => {
setAuthor(response.userId);
setMaxPpl(response.maxPpl);
setCurrentPpl(response.currentPpl);
setPath(JSON.parse(response.path));
setMiddlePoint(getMiddlePoint(JSON.parse(response.path)));
Comment on lines 86 to +90
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const [recruit, setRecruit] = useState<Recruit>();
// ...
const response = await get(`/recruit/${id}`);
setRecruit(response);
// ...

} catch {}
}, []);

Expand All @@ -67,7 +100,7 @@ const RecruitDetail = () => {
return (
<>
<Header loggedIn={true} text="모집 상세"></Header>
<ViewMap />
{renderMap()}
<Title>{title}</Title>
<Content>
<div>
Expand Down
1 change: 1 addition & 0 deletions client/src/types/MapProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ export interface MapProps {
* 숫자가 작을수록 zoom-in
*/
level?: number;
runningPath?: { lat: number; lng: number }[];
}