Displaying the current location on the whiteboard.

This commit is contained in:
Eric van der Vlist 2022-09-21 16:17:43 +02:00
parent f0cf255f14
commit 18c2819da9
5 changed files with 67 additions and 32 deletions

View File

@ -0,0 +1,26 @@
import React, { Fragment } from 'react';
import { useSelector } from 'react-redux';
import { MapState } from '../../store/map';
const CurrentLocation: React.FC<{}> = () => {
const scale = useSelector(
(state: { map: MapState }) => state.map.whiteboard.scale
);
const CurrentLocationState = useSelector(
(state: { map: MapState }) => state.map.currentLocation
);
return (
<circle
cx={CurrentLocationState.whiteboard.x}
cy={CurrentLocationState.whiteboard.y}
r={10 / scale}
fill='green'
opacity='50%'
></circle>
);
};
export default CurrentLocation;

View File

@ -23,6 +23,12 @@ const GetLocation: React.FC<{}> = () => {
lon: position.coords.longitude, lon: position.coords.longitude,
}) })
); );
dispatch(
mapActions.setCurrent({
lat: position.coords.latitude,
lon: position.coords.longitude,
})
);
}); });
}; };

View File

@ -8,6 +8,7 @@ import Slippy from '../slippy/slippy';
import TiledMap from './tiled-map'; import TiledMap from './tiled-map';
import GetLocation from './get-location'; import GetLocation from './get-location';
import Whiteboard from './whiteboard'; import Whiteboard from './whiteboard';
import CurrentLocation from './current-location';
const Map: react.FC<{}> = (props: {}) => { const Map: react.FC<{}> = (props: {}) => {
const dispatch = useDispatch(); const dispatch = useDispatch();
@ -29,7 +30,9 @@ const Map: react.FC<{}> = (props: {}) => {
<Fragment> <Fragment>
<Slippy> <Slippy>
<GetLocation /> <GetLocation />
<Whiteboard /> <Whiteboard>
<CurrentLocation />
</Whiteboard>
</Slippy> </Slippy>
<Layer> <Layer>
<TiledMap /> <TiledMap />

View File

@ -16,12 +16,6 @@ const Whiteboard: React.FC<WhiteboardProps> = (props: WhiteboardProps) => {
<g <g
transform={`scale(${whiteBoardState.scale}) translate(${whiteBoardState.translation.x},${whiteBoardState.translation.y})`} transform={`scale(${whiteBoardState.scale}) translate(${whiteBoardState.translation.x},${whiteBoardState.translation.y})`}
> >
<circle
cx={0.7154276391666667 * 2**zoom0}
cy={0.6137091783430285 * 2**zoom0}
r={10 / whiteBoardState.scale}
fill='green'
></circle>
{props.children} {props.children}
</g> </g>
</svg> </svg>

View File

@ -21,7 +21,7 @@ export interface MapScope {
} }
var initialMapScope: MapScope = { var initialMapScope: MapScope = {
center: { lat: -37.8403508, lon: 77.5539501 }, center: { lat: -37.8403508, lon: 77.5539501 },
zoom: 17, zoom: 15,
}; };
// Derived properties // Derived properties
@ -45,37 +45,21 @@ export interface WhiteboardState {
translation: Point; translation: Point;
} }
// Current location
export interface CurrentLocationState {
geo: geoPoint;
whiteboard: Point;
}
// Global state // Global state
export interface MapState { export interface MapState {
scope: MapScope; scope: MapScope;
tiles: TilesDescription; tiles: TilesDescription;
slippy: SlippyState; slippy: SlippyState;
whiteboard: WhiteboardState; whiteboard: WhiteboardState;
currentLocation: CurrentLocationState;
} }
/*
const nbTilesY = _.ceil(mapState.viewport.height / tileSize) + 4;
const nbTilesX = _.ceil(mapState.viewport.width / tileSize) + 4;
const [tileCenterY, reminderY] = lat2tile(
mapState.scope.center.lat,
mapState.scope.zoom
);
const [tileCenterX, reminderX] = lon2tile(
mapState.scope.center.lon,
mapState.scope.zoom
);
const firstTileY = tileCenterY - _.round(nbTilesY / 2);
const firstTileX = tileCenterX - _.round(nbTilesX / 2);
const locationY = (tileCenterY + reminderY - firstTileY) * tileSize;
const locationX = (tileCenterX + reminderX - firstTileX) * tileSize;
const targetLocationY = mapState.viewport.height / 2;
const targetLocationX = mapState.viewport.width / 2;
const deltaY = targetLocationY - locationY;
const deltaX = targetLocationX - locationX;
*/
export var initialMapState: MapState = { export var initialMapState: MapState = {
scope: initialMapScope, scope: initialMapScope,
slippy: { slippy: {
@ -103,6 +87,16 @@ export var initialMapState: MapState = {
y: 0, y: 0,
}, },
}, },
currentLocation: {
geo: {
lat: 0,
lon: 0,
},
whiteboard: {
x: 0,
y: 0,
},
},
}; };
const evaluateWhiteboardViewBox = ( const evaluateWhiteboardViewBox = (
@ -211,6 +205,18 @@ const mapSlice = createSlice({
state.scope.center.lon = action.payload.lon; state.scope.center.lon = action.payload.lon;
evaluateStateFromScope(state); evaluateStateFromScope(state);
}, },
setCurrent: (state, action) => {
state.currentLocation.geo.lat = action.payload.lat;
state.currentLocation.geo.lon = action.payload.lon;
state.currentLocation.whiteboard.x = lon2tile(
state.currentLocation.geo.lon,
zoom0
);
state.currentLocation.whiteboard.y = lat2tile(
state.currentLocation.geo.lat,
zoom0
);
},
shift: (state, action) => { shift: (state, action) => {
state.slippy.translation.x = state.slippy.translation.x =
state.slippy.translation.x + action.payload.x; state.slippy.translation.x + action.payload.x;