Skip to content

Commit 8228f12

Browse files
committed
show current location as blue dot - fix #430
1 parent c688588 commit 8228f12

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

src/App.tsx

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
getQueryStore,
1212
getRouteStore,
1313
getSettingsStore,
14+
getCurrentLocationStore,
1415
} from '@/stores/Stores'
1516
import MapComponent from '@/map/MapComponent'
1617
import MapOptions from '@/map/MapOptions'
@@ -22,6 +23,7 @@ import { QueryStoreState, RequestState } from '@/stores/QueryStore'
2223
import { RouteStoreState } from '@/stores/RouteStore'
2324
import { MapOptionsStoreState } from '@/stores/MapOptionsStore'
2425
import { ErrorStoreState } from '@/stores/ErrorStore'
26+
import { CurrentLocationStoreState } from '@/stores/CurrentLocationStore'
2527
import Search from '@/sidebar/search/Search'
2628
import ErrorMessage from '@/sidebar/ErrorMessage'
2729
import useBackgroundLayer from '@/layers/UseBackgroundLayer'
@@ -45,6 +47,7 @@ import useExternalMVTLayer from '@/layers/UseExternalMVTLayer'
4547
import LocationButton from '@/map/LocationButton'
4648
import { SettingsContext } from '@/contexts/SettingsContext'
4749
import usePOIsLayer from '@/layers/UsePOIsLayer'
50+
import useCurrentLocationLayer from '@/layers/UseCurrentLocationLayer'
4851

4952
export const POPUP_CONTAINER_ID = 'popup-container'
5053
export const SIDEBAR_CONTENT_ID = 'sidebar-content'
@@ -59,6 +62,7 @@ export default function App() {
5962
const [pathDetails, setPathDetails] = useState(getPathDetailsStore().state)
6063
const [mapFeatures, setMapFeatures] = useState(getMapFeatureStore().state)
6164
const [pois, setPOIs] = useState(getPOIsStore().state)
65+
const [currentLocation, setCurrentLocation] = useState(getCurrentLocationStore().state)
6266

6367
const map = getMap()
6468

@@ -72,6 +76,7 @@ export default function App() {
7276
const onPathDetailsChanged = () => setPathDetails(getPathDetailsStore().state)
7377
const onMapFeaturesChanged = () => setMapFeatures(getMapFeatureStore().state)
7478
const onPOIsChanged = () => setPOIs(getPOIsStore().state)
79+
const onCurrentLocationChanged = () => setCurrentLocation(getCurrentLocationStore().state)
7580

7681
getSettingsStore().register(onSettingsChanged)
7782
getQueryStore().register(onQueryChanged)
@@ -82,6 +87,7 @@ export default function App() {
8287
getPathDetailsStore().register(onPathDetailsChanged)
8388
getMapFeatureStore().register(onMapFeaturesChanged)
8489
getPOIsStore().register(onPOIsChanged)
90+
getCurrentLocationStore().register(onCurrentLocationChanged)
8591

8692
onQueryChanged()
8793
onInfoChanged()
@@ -91,6 +97,7 @@ export default function App() {
9197
onPathDetailsChanged()
9298
onMapFeaturesChanged()
9399
onPOIsChanged()
100+
onCurrentLocationChanged()
94101

95102
return () => {
96103
getSettingsStore().deregister(onSettingsChanged)
@@ -102,6 +109,7 @@ export default function App() {
102109
getPathDetailsStore().deregister(onPathDetailsChanged)
103110
getMapFeatureStore().deregister(onMapFeaturesChanged)
104111
getPOIsStore().deregister(onPOIsChanged)
112+
getCurrentLocationStore().deregister(onCurrentLocationChanged)
105113
}
106114
}, [])
107115

@@ -116,6 +124,7 @@ export default function App() {
116124
useQueryPointsLayer(map, query.queryPoints)
117125
usePathDetailsLayer(map, pathDetails)
118126
usePOIsLayer(map, pois)
127+
useCurrentLocationLayer(map, currentLocation)
119128

120129
const isSmallScreen = useMediaQuery({ query: '(max-width: 44rem)' })
121130
return (
@@ -138,6 +147,7 @@ export default function App() {
138147
error={error}
139148
encodedValues={info.encoded_values}
140149
drawAreas={settings.drawAreasEnabled}
150+
currentLocation={currentLocation}
141151
/>
142152
) : (
143153
<LargeScreenLayout
@@ -148,6 +158,7 @@ export default function App() {
148158
error={error}
149159
encodedValues={info.encoded_values}
150160
drawAreas={settings.drawAreasEnabled}
161+
currentLocation={currentLocation}
151162
/>
152163
)}
153164
</div>
@@ -163,9 +174,10 @@ interface LayoutProps {
163174
error: ErrorStoreState
164175
encodedValues: object[]
165176
drawAreas: boolean
177+
currentLocation: CurrentLocationStoreState
166178
}
167179

168-
function LargeScreenLayout({ query, route, map, error, mapOptions, encodedValues, drawAreas }: LayoutProps) {
180+
function LargeScreenLayout({ query, route, map, error, mapOptions, encodedValues, drawAreas, currentLocation }: LayoutProps) {
169181
const [showSidebar, setShowSidebar] = useState(true)
170182
const [showCustomModelBox, setShowCustomModelBox] = useState(false)
171183
return (
@@ -216,7 +228,7 @@ function LargeScreenLayout({ query, route, map, error, mapOptions, encodedValues
216228
<div className={styles.popupContainer} id={POPUP_CONTAINER_ID} />
217229
<div className={styles.onMapRightSide}>
218230
<MapOptions {...mapOptions} />
219-
<LocationButton queryPoints={query.queryPoints} />
231+
<LocationButton queryPoints={query.queryPoints} currentLocation={currentLocation} />
220232
</div>
221233
<div className={styles.map}>
222234
<MapComponent map={map} />
@@ -229,7 +241,7 @@ function LargeScreenLayout({ query, route, map, error, mapOptions, encodedValues
229241
)
230242
}
231243

232-
function SmallScreenLayout({ query, route, map, error, mapOptions, encodedValues, drawAreas }: LayoutProps) {
244+
function SmallScreenLayout({ query, route, map, error, mapOptions, encodedValues, drawAreas, currentLocation }: LayoutProps) {
233245
return (
234246
<>
235247
<div className={styles.smallScreenSidebar}>
@@ -248,7 +260,7 @@ function SmallScreenLayout({ query, route, map, error, mapOptions, encodedValues
248260
<div className={styles.smallScreenMapOptions}>
249261
<div className={styles.onMapRightSide}>
250262
<MapOptions {...mapOptions} />
251-
<LocationButton queryPoints={query.queryPoints} />
263+
<LocationButton queryPoints={query.queryPoints} currentLocation={currentLocation} />
252264
</div>
253265
</div>
254266

src/index.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
getQueryStore,
1414
getRouteStore,
1515
getSettingsStore,
16+
getCurrentLocationStore,
1617
setStores,
1718
} from '@/stores/Stores'
1819
import Dispatcher from '@/stores/Dispatcher'
@@ -31,6 +32,7 @@ import MapFeatureStore from '@/stores/MapFeatureStore'
3132
import SettingsStore from '@/stores/SettingsStore'
3233
import { ErrorAction, InfoReceived } from '@/actions/Actions'
3334
import POIsStore from '@/stores/POIsStore'
35+
import CurrentLocationStore from '@/stores/CurrentLocationStore'
3436
import { setDistanceFormat } from '@/Converters'
3537
import { AddressParseResult } from '@/pois/AddressParseResult'
3638

@@ -61,6 +63,7 @@ setStores({
6163
pathDetailsStore: new PathDetailsStore(),
6264
mapFeatureStore: new MapFeatureStore(),
6365
poisStore: new POIsStore(),
66+
currentLocationStore: new CurrentLocationStore(),
6467
})
6568

6669
setMap(createMap())
@@ -75,6 +78,7 @@ Dispatcher.register(getMapOptionsStore())
7578
Dispatcher.register(getPathDetailsStore())
7679
Dispatcher.register(getMapFeatureStore())
7780
Dispatcher.register(getPOIsStore())
81+
Dispatcher.register(getCurrentLocationStore())
7882

7983
// register map action receiver
8084
const smallScreenMediaQuery = window.matchMedia('(max-width: 44rem)')

src/map/LocationButton.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,20 @@ import LocationOn from '@/map/location_on.svg'
99
import { useState } from 'react'
1010
import { tr } from '@/translation/Translation'
1111
import { getBBoxFromCoord } from '@/utils'
12+
import { ToggleCurrentLocation } from '@/stores/CurrentLocationStore'
13+
import { CurrentLocationStoreState } from '@/stores/CurrentLocationStore'
1214

13-
export default function LocationButton(props: { queryPoints: QueryPoint[] }) {
15+
export default function LocationButton(props: { queryPoints: QueryPoint[]; currentLocation: CurrentLocationStoreState }) {
1416
const [locationSearch, setLocationSearch] = useState('synched_map_or_initial')
17+
1518
return (
1619
<div
1720
className={styles.locationOnOff}
1821
onClick={() => {
22+
// First toggle location display
23+
Dispatcher.dispatch(new ToggleCurrentLocation(!props.currentLocation.enabled))
24+
25+
// Then handle the location button click for routing/centering
1926
setLocationSearch('search')
2027
onCurrentLocationButtonClicked(coordinate => {
2128
if (coordinate) {

src/stores/Stores.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import PathDetailsStore from '@/stores/PathDetailsStore'
77
import MapFeatureStore from '@/stores/MapFeatureStore'
88
import SettingsStore from '@/stores/SettingsStore'
99
import POIsStore from '@/stores/POIsStore'
10+
import CurrentLocationStore from '@/stores/CurrentLocationStore'
1011

1112
let settingsStore: SettingsStore
1213
let queryStore: QueryStore
@@ -17,6 +18,7 @@ let mapOptionsStore: MapOptionsStore
1718
let pathDetailsStore: PathDetailsStore
1819
let mapFeatureStore: MapFeatureStore
1920
let poisStore: POIsStore
21+
let currentLocationStore: CurrentLocationStore
2022

2123
interface StoresInput {
2224
settingsStore: SettingsStore
@@ -28,6 +30,7 @@ interface StoresInput {
2830
pathDetailsStore: PathDetailsStore
2931
mapFeatureStore: MapFeatureStore
3032
poisStore: POIsStore
33+
currentLocationStore: CurrentLocationStore
3134
}
3235

3336
export const setStores = function (stores: StoresInput) {
@@ -40,6 +43,7 @@ export const setStores = function (stores: StoresInput) {
4043
pathDetailsStore = stores.pathDetailsStore
4144
mapFeatureStore = stores.mapFeatureStore
4245
poisStore = stores.poisStore
46+
currentLocationStore = stores.currentLocationStore
4347
}
4448

4549
export const getSettingsStore = () => settingsStore
@@ -51,3 +55,4 @@ export const getMapOptionsStore = () => mapOptionsStore
5155
export const getPathDetailsStore = () => pathDetailsStore
5256
export const getMapFeatureStore = () => mapFeatureStore
5357
export const getPOIsStore = () => poisStore
58+
export const getCurrentLocationStore = () => currentLocationStore

0 commit comments

Comments
 (0)