-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Use maplibre-gl - Basemap: Stamen/toner - Create Redux store
- Loading branch information
1 parent
a6b496c
commit ea172a7
Showing
7 changed files
with
2,828 additions
and
6,402 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { useRef, useState, useCallback, Fragment } from "react"; | ||
import { useSelector, useDispatch } from "react-redux"; | ||
import { mapActions } from "../../store/map-slice"; | ||
|
||
import maplibregl from "maplibre-gl"; | ||
import "maplibre-gl/dist/maplibre-gl.css"; | ||
import Map, { NavigationControl, ScaleControl } from "react-map-gl"; | ||
|
||
const MapView = () => { | ||
const dispatch = useDispatch(); | ||
const mapRef = useRef(); | ||
const [cursor, setCursor] = useState("auto"); | ||
const viewState = useSelector((state) => state.map.viewState); | ||
const mapStyle = useSelector((state) => state.map.mapStyle); | ||
|
||
const onMapLoad = useCallback(() => { | ||
mapRef.current.on("dragstart", () => { | ||
setCursor("move"); | ||
}); | ||
|
||
mapRef.current.on("dragend", () => { | ||
setCursor("auto"); | ||
}); | ||
}, []); | ||
|
||
const onMove = useCallback( | ||
(e) => { | ||
dispatch(mapActions.setViewState(e.viewState)); | ||
}, | ||
[dispatch] | ||
); | ||
|
||
return ( | ||
<Fragment> | ||
<Map | ||
{...viewState} | ||
mapStyle={mapStyle} | ||
mapLib={maplibregl} | ||
onLoad={onMapLoad} | ||
onMove={onMove} | ||
style={{ width: "100vw", height: "100vh" }} | ||
ref={mapRef} | ||
cursor={cursor} | ||
> | ||
<NavigationControl position="bottom-right" /> | ||
<ScaleControl /> | ||
</Map> | ||
</Fragment> | ||
); | ||
}; | ||
|
||
export default MapView; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { configureStore } from "@reduxjs/toolkit"; | ||
import mapSlice from "./map-slice"; | ||
|
||
const store = configureStore({ | ||
reducer: { | ||
map: mapSlice.reducer, | ||
}, | ||
middleware: (getDefaultMiddleware) => | ||
getDefaultMiddleware({ | ||
serializableCheck: false, | ||
}), | ||
}); | ||
|
||
export default store; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { createSlice } from "@reduxjs/toolkit"; | ||
|
||
const defaultViewState = { | ||
latitude: 25.02312941733473, | ||
longitude: 121.54838821352193, | ||
zoom: 14, | ||
customAttribution: | ||
'<a href="https://www.trendmicro.com/" target="_blank">© Trend Micro Inc.</a> ', | ||
}; | ||
|
||
const defaultMapStyle = { | ||
version: 8, | ||
sources: { | ||
stamen_toner: { | ||
type: "raster", | ||
tiles: [ | ||
"https://stamen-tiles.a.ssl.fastly.net/toner/{z}/{x}/{y}.png", | ||
], | ||
tileSize: 256, | ||
attribution: 'Map tiles by <a href="http://stamen.com">Stamen Design</a>, under <a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a>. Data by <a href="http://openstreetmap.org">OpenStreetMap</a>, under <a href="http://www.openstreetmap.org/copyright">ODbL</a>.', | ||
}, | ||
}, | ||
sprite: "", | ||
glyphs: "https://yuchuntsao.github.io/fonts/{fontstack}/{range}.pbf", | ||
layers: [ | ||
{ | ||
"id": "background", | ||
"type": "background", | ||
"minzoom": 0, | ||
"paint": { | ||
"background-color": "rgba(255, 255, 255, 1)" | ||
} | ||
}, | ||
{ | ||
id: "stamen_toner", | ||
type: "raster", | ||
source: "stamen_toner", | ||
paint: { | ||
'raster-opacity': 0.3 | ||
} | ||
} | ||
], | ||
}; | ||
|
||
const mapSlice = createSlice({ | ||
name: "map", | ||
initialState: { | ||
viewState: defaultViewState, | ||
mapStyle: defaultMapStyle, | ||
}, | ||
reducers: { | ||
setViewState(state, action) { | ||
state.viewState = action.payload; | ||
} | ||
}, | ||
}); | ||
|
||
export const mapActions = mapSlice.actions; | ||
export default mapSlice; |