From 8e2a68fb0f6efea45a0d07297be7aa9e897740b2 Mon Sep 17 00:00:00 2001 From: evlist Date: Wed, 2 Nov 2022 22:28:08 +0100 Subject: [PATCH] Displaying the current location. --- src/App.tsx | 22 ++------------ src/components/buttons/GetLocation.tsx | 6 ++++ src/components/map/CurrentLocation.tsx | 40 ++++++++++++++++++++++++++ src/components/map/LiveMap.tsx | 3 +- 4 files changed, 49 insertions(+), 22 deletions(-) create mode 100644 src/components/map/CurrentLocation.tsx diff --git a/src/App.tsx b/src/App.tsx index 5b911c0..b5041ed 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -36,7 +36,7 @@ import GetLocation from './components/buttons/GetLocation'; import { geoPoint } from './components/map/types'; import Back from './components/buttons/Back'; import Forward from './components/buttons/Forward'; -import Marker from './components/map/Marker'; +import CurrentLocation from './components/map/CurrentLocation'; setupIonicReact(); @@ -63,24 +63,6 @@ const App: React.FC = () => { const [scope, setScope] = useAtom(scopeAtom); console.log(`App, scope: ${JSON.stringify(scope)}`); - const marker = ( - - } - key='current' - /> - ); return ( @@ -89,7 +71,7 @@ const App: React.FC = () => { scope={scope} setScope={debounce(setScope, 1000)} numberOfTiledLayers={5} - markers={[marker]} + markers={[]} /> diff --git a/src/components/buttons/GetLocation.tsx b/src/components/buttons/GetLocation.tsx index 599617f..27195aa 100644 --- a/src/components/buttons/GetLocation.tsx +++ b/src/components/buttons/GetLocation.tsx @@ -7,9 +7,11 @@ import { IonButton, IonIcon } from '@ionic/react'; import { locateOutline } from 'ionicons/icons'; import { useAtom } from 'jotai'; import { setCenterAtom } from '../../App'; +import { locationAtom } from '../map/CurrentLocation'; const GetLocation: React.FC<{}> = () => { const [, setCenter] = useAtom(setCenterAtom); + const [, setLocation] = useAtom(locationAtom); const onClickHandler = (event: any) => { console.log('Click handler'); @@ -21,6 +23,10 @@ const GetLocation: React.FC<{}> = () => { lat: position.coords.latitude, lon: position.coords.longitude, }); + setLocation({ + lat: position.coords.latitude, + lon: position.coords.longitude, + }); }); }; diff --git a/src/components/map/CurrentLocation.tsx b/src/components/map/CurrentLocation.tsx new file mode 100644 index 0000000..0caa4e7 --- /dev/null +++ b/src/components/map/CurrentLocation.tsx @@ -0,0 +1,40 @@ +import { atom, useAtom } from 'jotai'; +import react from 'react'; +import Marker from './Marker'; +import { geoPoint, TileKeyObject } from './types'; + +export interface CurrentLocationProperties { + keyObject?: TileKeyObject; + zoom?: number; +} + +const initialLocation: geoPoint | null = null; +export const locationAtom = atom(initialLocation); + +export const CurrentLocation: react.FC = ( + props: CurrentLocationProperties +) => { + const [location] = useAtom(locationAtom); + return location !== null ? ( + + } + keyObject={props.keyObject} + zoom={props.zoom} + /> + ) : null; +}; + +export default CurrentLocation; diff --git a/src/components/map/LiveMap.tsx b/src/components/map/LiveMap.tsx index 4b6b96d..c9ce37e 100644 --- a/src/components/map/LiveMap.tsx +++ b/src/components/map/LiveMap.tsx @@ -1,10 +1,9 @@ -import react, { JSXElementConstructor, ReactComponentElement, ReactElement, ReactNode, useEffect, useState } from 'react'; +import react, { JSXElementConstructor, ReactElement, useEffect, useState } from 'react'; import useDimensions from 'react-cool-dimensions'; import { MapScope, Point } from './types'; import Map from './Map'; import Handlers from './Handlers'; -import Marker from './Marker'; import { tileProviders } from './tile-providers'; import { lon2tile, lat2tile, tile2lat, tile2long } from '../../lib/geo';