Skip to content

[UNI-141] feat : 길 조회 API 연결 #67

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

Merged
merged 1 commit into from
Feb 6, 2025
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
1 change: 1 addition & 0 deletions uniro_frontend/src/data/types/route.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { CautionIssueType, DangerIssueType } from "../../constant/enum/reportEnu
import { RoutePoint } from "../../constant/enum/routeEnum";
import { Coord } from "./coord";
import { MarkerTypes } from "./marker";
import { Node } from "./node";

export type RouteId = number;

Expand Down
115 changes: 64 additions & 51 deletions uniro_frontend/src/pages/reportHazard.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { useEffect, useState } from "react";
import useMap from "../hooks/useMap";
import { mockNavigationRoute } from "../data/mock/hanyangRoute";
import createAdvancedMarker from "../utils/markers/createAdvanedMarker";
import createMarkerElement from "../components/map/mapMarkers";
import { RouteEdge } from "../data/types/route";
import { CoreRoutesList } from "../data/types/route";
import { Markers } from "../constant/enum/markerEnum";
import { mockHazardEdges } from "../data/mock/hanyangHazardEdge";
import { ClickEvent } from "../data/types/event";
Expand All @@ -23,6 +22,9 @@ import BackButton from "../components/map/backButton";

import useUniversityInfo from "../hooks/useUniversityInfo";
import useRedirectUndefined from "../hooks/useRedirectUndefined";
import { University } from "../data/types/university";
import { useSuspenseQuery } from "@tanstack/react-query";
import { getAllRoutes } from "../api/route";

interface reportMarkerTypes extends MarkerTypesWithElement {
edge: [google.maps.LatLng | google.maps.LatLngLiteral, google.maps.LatLng | google.maps.LatLngLiteral];
Expand All @@ -36,7 +38,12 @@ export default function ReportHazardPage() {
const [message, setMessage] = useState<ReportHazardMessage>(ReportHazardMessage.DEFAULT);

const { university } = useUniversityInfo();
useRedirectUndefined<string | undefined>([university]);
useRedirectUndefined<University | undefined>([university]);

const { data } = useSuspenseQuery({
queryKey: ["routes", university?.id],
queryFn: () => getAllRoutes(university?.id ?? 1001),
});

const resetMarker = (prevMarker: MarkerTypesWithElement) => {
if (prevMarker.type === Markers.REPORT) {
Expand Down Expand Up @@ -85,72 +92,78 @@ export default function ReportHazardPage() {
}
};

const drawRoute = (routes: RouteEdge[]) => {
const drawRoute = (coreRouteList: CoreRoutesList) => {
if (!Polyline || !AdvancedMarker || !map) return;

for (const route of routes) {
const coreNode = route.endNode;
for (const coreRoutes of coreRouteList) {
// 가장 끝쪽 Core Node 그리기
const endNode = coreRoutes.routes[coreRoutes.routes.length - 1].node2;

createAdvancedMarker(
AdvancedMarker,
map,
coreNode,
endNode,
createMarkerElement({ type: Markers.WAYPOINT, className: "translate-waypoint" }),
);

const routePolyLine = new Polyline({
map: map,
path: [route.startNode, route.endNode],
strokeColor: "#808080",
});
for (const coreRoute of coreRoutes.routes) {
const routePolyLine = new Polyline({
map: map,
path: [coreRoute.node1, coreRoute.node2],
strokeColor: "#808080",
});

routePolyLine.addListener("click", (e: ClickEvent) => {
const subNodes = createSubNodes(routePolyLine);
routePolyLine.addListener("click", (e: ClickEvent) => {
const subNodes = createSubNodes(routePolyLine);

const edges = subNodes
.map(
(node, idx) =>
[node, subNodes[idx + 1]] as [google.maps.LatLngLiteral, google.maps.LatLngLiteral],
)
.slice(0, -1);
alert(coreRoute.routeId);

const point = LatLngToLiteral(e.latLng);
const edges = subNodes
.map(
(node, idx) =>
[node, subNodes[idx + 1]] as [google.maps.LatLngLiteral, google.maps.LatLngLiteral],
)
.slice(0, -1);

const { edge: nearestEdge, point: nearestPoint } = findNearestSubEdge(edges, point);
const point = LatLngToLiteral(e.latLng);

const newReportMarker = createAdvancedMarker(
AdvancedMarker,
map,
centerCoordinate(nearestEdge[0], nearestEdge[1]),
createMarkerElement({
type: Markers.REPORT,
className: "translate-routemarker",
hasAnimation: true,
}),
);
const { edge: nearestEdge, point: nearestPoint } = findNearestSubEdge(edges, point);

setMessage(ReportHazardMessage.CREATE);
const newReportMarker = createAdvancedMarker(
AdvancedMarker,
map,
centerCoordinate(nearestEdge[0], nearestEdge[1]),
createMarkerElement({
type: Markers.REPORT,
className: "translate-routemarker",
hasAnimation: true,
}),
);

setReportMarker((prevMarker) => {
if (prevMarker) {
resetMarker(prevMarker);
}
setMessage(ReportHazardMessage.CREATE);

setReportMarker((prevMarker) => {
if (prevMarker) {
resetMarker(prevMarker);
}

return {
type: Markers.REPORT,
element: newReportMarker,
edge: nearestEdge,
};
return {
type: Markers.REPORT,
element: newReportMarker,
edge: nearestEdge,
};
});
});
});
}
// 시작하는 쪽의 Core Node 그리기
const startNode = coreRoutes.routes[0].node1;
createAdvancedMarker(
AdvancedMarker,
map,
startNode,
createMarkerElement({ type: Markers.WAYPOINT, className: "translate-waypoint" }),
);
}

createAdvancedMarker(
AdvancedMarker,
map,
routes[0].startNode,
createMarkerElement({ type: Markers.WAYPOINT, className: "translate-waypoint" }),
);
};

const reportHazard = () => {
Expand All @@ -162,7 +175,7 @@ export default function ReportHazardPage() {
};

useEffect(() => {
drawRoute(mockNavigationRoute.route);
drawRoute(data);
addHazardMarker();

if (map) {
Expand Down
166 changes: 87 additions & 79 deletions uniro_frontend/src/pages/reportRoute.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { useEffect, useRef, useState } from "react";
import createMarkerElement from "../components/map/mapMarkers";
import { Markers } from "../constant/enum/markerEnum";
import { RouteEdge } from "../data/types/route";
import useMap from "../hooks/useMap";
import createAdvancedMarker from "../utils/markers/createAdvanedMarker";
import { mockNavigationRoute } from "../data/mock/hanyangRoute";
import { ClickEvent } from "../data/types/event";
import createSubNodes from "../utils/polylines/createSubnodes";
import { LatLngToLiteral } from "../utils/coordinates/coordinateTransform";
Expand All @@ -17,6 +15,10 @@ import toggleMarkers from "../utils/markers/toggleMarkers";
import BackButton from "../components/map/backButton";
import useUniversityInfo from "../hooks/useUniversityInfo";
import useRedirectUndefined from "../hooks/useRedirectUndefined";
import { University } from "../data/types/university";
import { useSuspenseQuery } from "@tanstack/react-query";
import { getAllRoutes } from "../api/route";
import { CoreRoutesList } from "../data/types/route";

const colors = [
"#f1a2b3",
Expand Down Expand Up @@ -50,7 +52,12 @@ export default function ReportRoutePage() {
const [isCautionAcitve, setIsCautionActive] = useState<boolean>(true);

const { university } = useUniversityInfo();
useRedirectUndefined<string | undefined>([university]);
useRedirectUndefined<University | undefined>([university]);

const { data } = useSuspenseQuery({
queryKey: ["routes", university?.id],
queryFn: () => getAllRoutes(university?.id ?? 1001),
});

const addHazardMarker = () => {
if (AdvancedMarker === null || map === null) return;
Expand Down Expand Up @@ -119,97 +126,98 @@ export default function ReportRoutePage() {
newPolyLine.current.setPath([]);
};

const drawRoute = (routes: RouteEdge[]) => {
const drawRoute = (coreRouteList: CoreRoutesList) => {
if (!Polyline || !AdvancedMarker || !map) return;

for (const route of routes) {
const coreNode = route.endNode;
for (const coreRoutes of coreRouteList) {
// 가장 끝쪽 Core Node 그리기
const endNode = coreRoutes.routes[coreRoutes.routes.length - 1].node2;

createAdvancedMarker(
AdvancedMarker,
map,
coreNode,
endNode,
createMarkerElement({ type: Markers.WAYPOINT, className: "translate-waypoint" }),
);
for (const coreRoute of coreRoutes.routes) {
const routePolyLine = new Polyline({
map: map,
path: [coreRoute.node1, coreRoute.node2],
strokeColor: "#808080",
});
Comment on lines +129 to +147
Copy link
Contributor

Choose a reason for hiding this comment

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

LGTM!
좋습니다! 다행히 로직들이 정상적으로 동작해서 다행입니다..!

coreRouteList, CoreRoute 등 적절한 타입 덕분에 코드 가독이 좋습니다!

routePolyLine.addListener("click", (e: ClickEvent) => {
const subNodes = createSubNodes(routePolyLine);

const routePolyLine = new Polyline({
map: map,
path: [route.startNode, route.endNode],
strokeColor: "#808080",
});

routePolyLine.addListener("click", (e: ClickEvent) => {
const subNodes = createSubNodes(routePolyLine);

const edges = subNodes
.map(
(node, idx) =>
[node, subNodes[idx + 1]] as [google.maps.LatLngLiteral, google.maps.LatLngLiteral],
)
.slice(0, -1);

const point = LatLngToLiteral(e.latLng);
const edges = subNodes
.map(
(node, idx) =>
[node, subNodes[idx + 1]] as [google.maps.LatLngLiteral, google.maps.LatLngLiteral],
)
.slice(0, -1);

const { edge: nearestEdge, point: nearestPoint } = findNearestSubEdge(edges, point);
const point = LatLngToLiteral(e.latLng);

const tempWaypointMarker = createAdvancedMarker(
AdvancedMarker,
map,
nearestPoint,
createMarkerElement({
type: Markers.WAYPOINT,
className: "translate-waypoint",
}),
);
const { edge: nearestEdge, point: nearestPoint } = findNearestSubEdge(edges, point);

if (originPoint.current) {
setNewPoints((prevPoints) => {
if (prevPoints.element) {
prevPoints.element.position = nearestPoint;
return {
...prevPoints,
coords: [...prevPoints.coords, nearestPoint],
};
} else {
setIsActive(true);
return {
element: new AdvancedMarker({
map: map,
position: nearestPoint,
content: createMarkerElement({
type: Markers.DESTINATION,
const tempWaypointMarker = createAdvancedMarker(
AdvancedMarker,
map,
nearestPoint,
createMarkerElement({
type: Markers.WAYPOINT,
className: "translate-waypoint",
}),
);
if (originPoint.current) {
setNewPoints((prevPoints) => {
if (prevPoints.element) {
prevPoints.element.position = nearestPoint;
return {
...prevPoints,
coords: [...prevPoints.coords, nearestPoint],
};
} else {
setIsActive(true);
return {
element: new AdvancedMarker({
map: map,
position: nearestPoint,
content: createMarkerElement({
type: Markers.DESTINATION,
}),
}),
}),
coords: [...prevPoints.coords, nearestPoint],
};
}
});
} else {
const originMarker = new AdvancedMarker({
map: map,
position: nearestPoint,
content: createMarkerElement({ type: Markers.ORIGIN }),
});
coords: [...prevPoints.coords, nearestPoint],
};
}
});
} else {
const originMarker = new AdvancedMarker({
map: map,
position: nearestPoint,
content: createMarkerElement({ type: Markers.ORIGIN }),
});

originPoint.current = {
point: nearestPoint,
element: originMarker,
};
originPoint.current = {
point: nearestPoint,
element: originMarker,
};

setNewPoints({
element: null,
coords: [nearestPoint],
});
}
});
}
setNewPoints({
element: null,
coords: [nearestPoint],
});
}
});
}
const startNode = coreRoutes.routes[0].node1;

createAdvancedMarker(
AdvancedMarker,
map,
routes[0].startNode,
createMarkerElement({ type: Markers.WAYPOINT, className: "translate-waypoint" }),
);
createAdvancedMarker(
AdvancedMarker,
map,
startNode,
createMarkerElement({ type: Markers.WAYPOINT, className: "translate-waypoint" }),
);
}
};

useEffect(() => {
Expand All @@ -219,7 +227,7 @@ export default function ReportRoutePage() {
}, [newPoints]);

useEffect(() => {
drawRoute(mockNavigationRoute.route);
drawRoute(data);
addHazardMarker();
if (Polyline) {
newPolyLine.current = new Polyline({ map: map, path: [], strokeColor: "#0367FB" });
Expand Down