detect map hash being passed more reliably to auto-set bounds [#246] (#251)

* add README for installing /app [#244]
This commit is contained in:
Brandon Liu
2023-09-11 11:05:50 +08:00
committed by GitHub
parent 009cd20ff4
commit 70146469e9
4 changed files with 19 additions and 9 deletions

4
app/README.md Normal file
View File

@@ -0,0 +1,4 @@
# Install
* First go to the `/js` directory in the root of the PMTiles repository and `npm install`
* `npm run dev` to start the app on port 3000

View File

@@ -59,6 +59,7 @@ function App() {
let [errorDisplay, setErrorDisplay] = useState<string | undefined>(); let [errorDisplay, setErrorDisplay] = useState<string | undefined>();
let [file, setFile] = useState<PMTiles | undefined>(); let [file, setFile] = useState<PMTiles | undefined>();
let [mapHashPassed, setMapHashPassed] = useState<boolean>(false);
// initial load // initial load
useEffect(() => { useEffect(() => {
@@ -67,6 +68,9 @@ function App() {
let initialValue = new PMTiles(loadUrl); let initialValue = new PMTiles(loadUrl);
setFile(initialValue); setFile(initialValue);
} }
if (location.hash.includes("map")) {
setMapHashPassed(true);
}
}, []); }, []);
useEffect(() => { useEffect(() => {
@@ -110,7 +114,11 @@ function App() {
</GithubA> </GithubA>
</GithubLink> </GithubLink>
</Header> </Header>
{file ? <Loader file={file} /> : <Start setFile={setFile} />} {file ? (
<Loader file={file} mapHashPassed={mapHashPassed} />
) : (
<Start setFile={setFile} />
)}
<DialogPrimitive.Root open={errorDisplay !== undefined}> <DialogPrimitive.Root open={errorDisplay !== undefined}>
<DialogPrimitive.Portal> <DialogPrimitive.Portal>
<StyledOverlay /> <StyledOverlay />

View File

@@ -70,12 +70,14 @@ const ToolbarLink = StyledLink;
const ToolbarToggleGroup = StyledToggleGroup; const ToolbarToggleGroup = StyledToggleGroup;
const ToolbarToggleItem = StyledToggleItem; const ToolbarToggleItem = StyledToggleItem;
function Loader(props: { file: PMTiles }) { function Loader(props: { file: PMTiles; mapHashPassed: boolean }) {
let [tab, setTab] = useState("maplibre"); let [tab, setTab] = useState("maplibre");
let view; let view;
if (tab === "maplibre") { if (tab === "maplibre") {
view = <MaplibreMap file={props.file} />; view = (
<MaplibreMap file={props.file} mapHashPassed={props.mapHashPassed} />
);
} else if (tab === "inspector") { } else if (tab === "inspector") {
view = <Inspector file={props.file} />; view = <Inspector file={props.file} />;
} else { } else {

View File

@@ -319,7 +319,7 @@ const vectorStyle = async (file: PMTiles): Promise<any> => {
}; };
}; };
function MaplibreMap(props: { file: PMTiles }) { function MaplibreMap(props: { file: PMTiles; mapHashPassed: boolean }) {
let mapContainerRef = useRef<HTMLDivElement>(null); let mapContainerRef = useRef<HTMLDivElement>(null);
let [hamburgerOpen, setHamburgerOpen] = useState<boolean>(true); let [hamburgerOpen, setHamburgerOpen] = useState<boolean>(true);
let [showAttributes, setShowAttributes] = useState<boolean>(false); let [showAttributes, setShowAttributes] = useState<boolean>(false);
@@ -437,11 +437,7 @@ function MaplibreMap(props: { file: PMTiles }) {
if (mapRef.current) { if (mapRef.current) {
let map = mapRef.current; let map = mapRef.current;
let header = await props.file.getHeader(); let header = await props.file.getHeader();
if ( if (!props.mapHashPassed) {
map.getCenter().lng === INITIAL_LNG &&
map.getCenter().lat == INITIAL_LAT &&
map.getZoom() === INITIAL_ZOOM
) {
// the map hash was not passed, so auto-detect the initial viewport based on metadata // the map hash was not passed, so auto-detect the initial viewport based on metadata
map.fitBounds( map.fitBounds(
[ [