-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[]>([]); | ||
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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(); | ||
|
@@ -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}"`; | ||
|
@@ -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}`); | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 {} | ||
}, []); | ||
|
||
|
@@ -67,7 +100,7 @@ const RecruitDetail = () => { | |
return ( | ||
<> | ||
<Header loggedIn={true} text="모집 상세"></Header> | ||
<ViewMap /> | ||
{renderMap()} | ||
<Title>{title}</Title> | ||
<Content> | ||
<div> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
showMap에서는 path를 state로 관리할 필요가 없는 것 같아요