mirror of
https://github.com/muerwre/orchidmap-front.git
synced 2025-04-25 02:56:41 +07:00
router now has additional end marker
This commit is contained in:
parent
5db6d85e35
commit
24641a33f7
2 changed files with 22 additions and 17 deletions
|
@ -2,7 +2,7 @@ import { FC, useEffect, useCallback, memo, useState } from 'react';
|
||||||
import { OsrmRouter } from '~/utils/map/OsrmRouter';
|
import { OsrmRouter } from '~/utils/map/OsrmRouter';
|
||||||
import { connect } from 'react-redux';
|
import { connect } from 'react-redux';
|
||||||
import { selectMapRoute } from '~/redux/map/selectors';
|
import { selectMapRoute } from '~/redux/map/selectors';
|
||||||
import { selectEditorRouter, selectEditorMode } from '~/redux/editor/selectors';
|
import { selectEditorRouter, selectEditorMode, selectEditorDistance } from '~/redux/editor/selectors';
|
||||||
import { MainMap } from '~/constants/map';
|
import { MainMap } from '~/constants/map';
|
||||||
import * as EDITOR_ACTIONS from '~/redux/editor/actions';
|
import * as EDITOR_ACTIONS from '~/redux/editor/actions';
|
||||||
import { MODES } from '~/constants/modes';
|
import { MODES } from '~/constants/modes';
|
||||||
|
@ -14,6 +14,7 @@ const mapStateToProps = state => ({
|
||||||
route: selectMapRoute(state),
|
route: selectMapRoute(state),
|
||||||
router: selectEditorRouter(state),
|
router: selectEditorRouter(state),
|
||||||
mode: selectEditorMode(state),
|
mode: selectEditorMode(state),
|
||||||
|
distance: selectEditorDistance(state),
|
||||||
});
|
});
|
||||||
|
|
||||||
const mapDispatchToProps = {
|
const mapDispatchToProps = {
|
||||||
|
@ -23,38 +24,37 @@ const mapDispatchToProps = {
|
||||||
type Props = ReturnType<typeof mapStateToProps> & typeof mapDispatchToProps & {};
|
type Props = ReturnType<typeof mapStateToProps> & typeof mapDispatchToProps & {};
|
||||||
|
|
||||||
const RouterUnconnected: FC<Props> = memo(
|
const RouterUnconnected: FC<Props> = memo(
|
||||||
({ route, mode, router: { waypoints }, editorSetRouter }) => {
|
({ route, mode, router: { waypoints }, editorSetRouter, distance }) => {
|
||||||
const [distance, setDistance] = useState(0);
|
const [dist, setDist] = useState(0);
|
||||||
const [end, setEnd] = useState<LatLngLiteral>(null);
|
const [end, setEnd] = useState<LatLngLiteral>(null);
|
||||||
const [direction, setDirection] = useState<boolean>(false);
|
const [direction, setDirection] = useState<boolean>(false);
|
||||||
|
|
||||||
const updateWaypoints = useCallback(
|
const updateWaypoints = useCallback(
|
||||||
({ waypoints }) => {
|
({ waypoints }) => {
|
||||||
const filtered = waypoints.filter(wp => !!wp.latLng);
|
const filtered = waypoints.filter(wp => !!wp.latLng);
|
||||||
|
console.log('waypoints updated: ', filtered.length);
|
||||||
|
|
||||||
if (filtered.length < 2) {
|
if (filtered.length < 2) {
|
||||||
setDistance(0);
|
setDist(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
editorSetRouter({ waypoints: filtered });
|
editorSetRouter({ waypoints: filtered });
|
||||||
},
|
},
|
||||||
[editorSetRouter, setDistance]
|
[editorSetRouter, setDist]
|
||||||
);
|
);
|
||||||
|
|
||||||
const updateDistance = useCallback(
|
const updateDistance = useCallback(
|
||||||
({ routes, waypoints }) => {
|
({ routes, waypoints }) => {
|
||||||
console.log(routes.length, waypoints.length);
|
console.log(routes.length, waypoints.length);
|
||||||
if (!routes || !routes.length || waypoints.length < 2) {
|
if (!routes || !routes.length || waypoints.length < 2) {
|
||||||
console.log('hm 1');
|
setDist(0);
|
||||||
setDistance(0);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const { summary, coordinates } = routes[0];
|
const { summary, coordinates } = routes[0];
|
||||||
|
|
||||||
if (coordinates.length <= 1) {
|
if (coordinates.length <= 1) {
|
||||||
console.log('hm 2');
|
setDist(0);
|
||||||
setDistance(0);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -66,15 +66,16 @@ const RouterUnconnected: FC<Props> = memo(
|
||||||
MainMap.latLngToContainerPoint(coordinates[coordinates.length - 3])
|
MainMap.latLngToContainerPoint(coordinates[coordinates.length - 3])
|
||||||
);
|
);
|
||||||
|
|
||||||
setDistance(totalDistance);
|
setDist(totalDistance);
|
||||||
setEnd(latlng);
|
setEnd(latlng);
|
||||||
setDirection(angle > -90 && angle < 90);
|
setDirection(angle > -90 && angle < 90);
|
||||||
},
|
},
|
||||||
[setDistance, setEnd]
|
[setDist, setEnd]
|
||||||
);
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
OsrmRouter.on('waypointschanged', updateWaypoints)
|
OsrmRouter.on('waypointschanged', updateWaypoints)
|
||||||
|
.on('waypointschanged', console.log)
|
||||||
.on('routesfound', updateDistance)
|
.on('routesfound', updateDistance)
|
||||||
.addTo(MainMap);
|
.addTo(MainMap);
|
||||||
|
|
||||||
|
@ -84,7 +85,7 @@ const RouterUnconnected: FC<Props> = memo(
|
||||||
}, [MainMap, updateWaypoints, mode]);
|
}, [MainMap, updateWaypoints, mode]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!distance || !end) {
|
if (!dist || !end) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,7 +95,7 @@ const RouterUnconnected: FC<Props> = memo(
|
||||||
icon: divIcon({
|
icon: divIcon({
|
||||||
html: `
|
html: `
|
||||||
<div class="leaflet-km-dist">
|
<div class="leaflet-km-dist">
|
||||||
${parseFloat(distance.toFixed(1))}
|
${parseFloat((distance + dist).toFixed(1))}
|
||||||
</div>
|
</div>
|
||||||
`,
|
`,
|
||||||
className: classNames('leaflet-km-marker router-end-marker', { right: !direction }),
|
className: classNames('leaflet-km-marker router-end-marker', { right: !direction }),
|
||||||
|
@ -109,10 +110,13 @@ const RouterUnconnected: FC<Props> = memo(
|
||||||
return () => {
|
return () => {
|
||||||
item.removeFrom(MainMap);
|
item.removeFrom(MainMap);
|
||||||
};
|
};
|
||||||
}, [distance, end, direction]);
|
}, [dist, end, direction, distance]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (mode !== MODES.ROUTER) return;
|
if (mode !== MODES.ROUTER) {
|
||||||
|
setDist(0);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const wp = OsrmRouter.getWaypoints()
|
const wp = OsrmRouter.getWaypoints()
|
||||||
.filter(point => point.latLng)
|
.filter(point => point.latLng)
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { IState } from "../store";
|
import { IState } from '../store';
|
||||||
|
|
||||||
export const selectEditor = (state: IState) => state.editor;
|
export const selectEditor = (state: IState) => state.editor;
|
||||||
export const selectEditorSave = (state: IState) => state.editor.save;
|
export const selectEditorSave = (state: IState) => state.editor.save;
|
||||||
|
@ -6,4 +6,5 @@ export const selectEditorEditing = (state: IState) => state.editor.editing;
|
||||||
export const selectEditorMode = (state: IState) => state.editor.mode;
|
export const selectEditorMode = (state: IState) => state.editor.mode;
|
||||||
export const selectEditorActiveSticker = (state: IState) => state.editor.activeSticker;
|
export const selectEditorActiveSticker = (state: IState) => state.editor.activeSticker;
|
||||||
export const selectEditorRenderer = (state: IState) => state.editor.renderer;
|
export const selectEditorRenderer = (state: IState) => state.editor.renderer;
|
||||||
export const selectEditorRouter = (state: IState) => state.editor.router;
|
export const selectEditorRouter = (state: IState) => state.editor.router;
|
||||||
|
export const selectEditorDistance = (state: IState) => state.editor.distance;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue