Skip to content

Commit

Permalink
fix drag-and-drop of tilesets. (#353)
Browse files Browse the repository at this point in the history
* also show current zoom in bottom left of app view.
  • Loading branch information
bdon authored Jan 9, 2025
1 parent c49a479 commit b094129
Showing 1 changed file with 30 additions and 25 deletions.
55 changes: 30 additions & 25 deletions app/src/MapView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ import type {
StyleSpecification,
} from "maplibre-gl";
import "maplibre-gl/dist/maplibre-gl.css";
import type { LayerSpecification } from "@maplibre/maplibre-gl-style-spec";
import type {
LayerSpecification,
VectorSourceSpecification,
} from "@maplibre/maplibre-gl-style-spec";
import { FileSource, PMTiles, Protocol } from "pmtiles";
import {
For,
Expand Down Expand Up @@ -124,8 +127,6 @@ function getMaplibreStyle(
tiles?: string,
npmLayers?: LayerSpecification[],
droppedArchive?: PMTiles,
minZoom?: number,
maxZoom?: number,
): StyleSpecification {
const style = {
version: 8 as unknown,
Expand Down Expand Up @@ -156,8 +157,6 @@ function getMaplibreStyle(
type: "vector",
attribution: ATTRIBUTION,
tiles: [`pmtiles://${droppedArchive.source.getKey()}/{z}/{x}/{y}`],
minzoom: minZoom,
maxzoom: maxZoom,
},
};
} else {
Expand Down Expand Up @@ -230,6 +229,7 @@ function MapLibreView(props: {

const [error, setError] = createSignal<string | undefined>();
const [timelinessInfo, setTimelinessInfo] = createSignal<string>();
const [zoom, setZoom] = createSignal<number>(0);

onMount(() => {
props.ref?.({ fit });
Expand Down Expand Up @@ -297,6 +297,7 @@ function MapLibreView(props: {
});

map.on("idle", () => {
setZoom(map.getZoom());
setError(undefined);
if (protocolRef && props.tiles) {
const p = protocolRef.tiles.get(props.tiles);
Expand Down Expand Up @@ -341,6 +342,10 @@ function MapLibreView(props: {
clearTimeout(longPressTimeout);
};

map.on("zoom", (e) => {
setZoom(e.target.getZoom());
});

map.on("touchend", clearLongPress);
map.on("touchcancel", clearLongPress);
map.on("touchmove", clearLongPress);
Expand Down Expand Up @@ -387,30 +392,30 @@ function MapLibreView(props: {
}
});

createEffect(() => {
// ensure the dropped archive is first added to the protocol
if (protocolRef && props.droppedArchive) {
protocolRef.add(props.droppedArchive);
}
});

createEffect(async () => {
// HACK: do this to ensure a tracking scope is created
// because async effects are not correct
[props.theme, props.lang, props.localSprites, props.tiles, props.npmLayers];
const style = getMaplibreStyle(
props.theme,
props.lang,
props.localSprites,
props.tiles,
props.npmLayers,
props.droppedArchive,
);
if (mapRef) {
let minZoom: number | undefined;
let maxZoom: number | undefined;
if (props.droppedArchive) {
const header = await props.droppedArchive.getHeader();
minZoom = header.minZoom;
maxZoom = header.maxZoom;
const source = style.sources.protomaps as VectorSourceSpecification;
source.minzoom = header.minZoom;
source.maxzoom = header.maxZoom;
}
mapRef.setStyle(
getMaplibreStyle(
props.theme,
props.lang,
props.localSprites,
props.tiles,
props.npmLayers,
props.droppedArchive,
minZoom,
maxZoom,
),
);
mapRef.setStyle(style);
}
});

Expand All @@ -419,7 +424,7 @@ function MapLibreView(props: {
<div class="hidden" ref={hiddenRef} />
<div ref={mapContainer} class="h-full w-full flex" />
<div class="absolute bottom-0 p-1 text-xs bg-white bg-opacity-50">
{timelinessInfo()}
{timelinessInfo()} z@{zoom().toFixed(2)}
</div>
<Show when={error()}>
<div class="absolute h-20 w-full flex justify-center items-center bg-white bg-opacity-50 font-mono text-red">
Expand Down

0 comments on commit b094129

Please sign in to comment.