Skip to content

Build your personal knowledge base with Trilium Notes

fix(geomap): frame a searched place on the ground it covers

Elian Doran233e88c18b98
diff --git a/apps/client/src/widgets/collections/geomap/SearchBox.spec.tsx b/apps/client/src/widgets/collections/geomap/SearchBox.spec.tsx
index 0486240e90..354e354169 100644
--- a/apps/client/src/widgets/collections/geomap/SearchBox.spec.tsx
+++ b/apps/client/src/widgets/collections/geomap/SearchBox.spec.tsx
@@ -27,7 +27,13 @@ vi.mock("../../../services/i18n", () => ({
t: (key: string, vars?: Record<string, unknown>) => (vars ? `${key}(${Object.values(vars).join(",")})` : key)
}));
-const TOKYO: GeoSearchResult = { id: "1", label: "Tokyo, Japan", name: "Tokyo", lat: 35.6762, lng: 139.6503 };
+const TOKYO: GeoSearchResult = {
+ id: "1", label: "Tokyo, Japan", name: "Tokyo", lat: 35.6762, lng: 139.6503,
+ bounds: [ [ 139.5, 35.5 ], [ 139.9, 35.8 ] ]
+};
+
+/** A result the geocoder gave no extent for, which is what the fallback zoom is left for. */
+const UNBOUNDED: GeoSearchResult = { id: "9", label: "Somewhere", name: "Somewhere", lat: 10, lng: 20 };
/** Stands in for the geocoder, which would otherwise reach for the network. */
function mockGeocoder(results: GeoSearchResult[]) {
@@ -35,9 +41,9 @@ function mockGeocoder(results: GeoSearchResult[]) {
.mockResolvedValue(results);
}
-/** A map that can be flown somewhere, which is all the bar uses. */
+/** A map that can be flown somewhere or framed on something, which is all the bar uses. */
function fakeMap() {
- return { flyTo: vi.fn() };
+ return { flyTo: vi.fn(), fitBounds: vi.fn() };
}
/** Two notes on the map and one that is only in its subtree, having no location to be drawn at. */
@@ -158,18 +164,38 @@ describe("geo map SearchBox", () => {
expect(entries()).toHaveLength(0);
});
- it("flies to a geocoded place, further out than to a marker", async () => {
+ it("frames a place on the ground it covers, so a street is not shown as its city", async () => {
mockGeocoder([ TOKYO ]);
const map = fakeMap();
- const container = renderSearchBox(map, mapNotes());
+ const container = renderSearchBox(map, []);
await type(container, "tokyo");
- await pick(1);
- await pick(1);
+ await pick(0);
+ await pick(0);
- expect(map.flyTo).toHaveBeenCalledWith({ center: [ 139.6503, 35.6762 ], zoom: expect.any(Number) });
+ expect(map.fitBounds).toHaveBeenCalledWith(TOKYO.bounds, {
+ padding: expect.any(Number),
+ // A house covers a few metres, which framed on its own would fill the screen with a roof.
+ maxZoom: expect.any(Number)
+ });
+ expect(map.flyTo).not.toHaveBeenCalled();
expect(field(container).value).toBe("Tokyo, Japan");
+ });
+
+ it("falls back to a zoom for a place the geocoder gave no extent for", async () => {
+ mockGeocoder([ UNBOUNDED ]);
+ const map = fakeMap();
+ const container = renderSearchBox(map, mapNotes());
+
+ await type(container, "somewhere");
+ await pick(0);
+ await pick(0);
+
+ expect(map.fitBounds).not.toHaveBeenCalled();
+ expect(map.flyTo).toHaveBeenCalledWith({ center: [ UNBOUNDED.lng, UNBOUNDED.lat ], zoom: expect.any(Number) });
+ // A note marks a spot rather than an area, so it is shown closer in than a place of unknown
+ // size.
const [ { zoom: placeZoom } ] = map.flyTo.mock.calls[0];
await type(container, "tokyo trip");
await pick(0);
diff --git a/apps/client/src/widgets/collections/geomap/SearchBox.tsx b/apps/client/src/widgets/collections/geomap/SearchBox.tsx
index 84c92e984e..b2c882112d 100644
--- a/apps/client/src/widgets/collections/geomap/SearchBox.tsx
+++ b/apps/client/src/widgets/collections/geomap/SearchBox.tsx
@@ -16,9 +16,18 @@ import PlaceMarker from "./PlaceMarker";
/** Shorter queries are not searched. */
const MIN_QUERY_LENGTH = 2;
-/** The zoom level a place from the geocoder is shown at. */
+/** The zoom level a place is shown at where the geocoder does not say how much ground it covers. */
const PLACE_ZOOM = 12;
+/**
+ * How close a place is framed at most. A house's extent is a few metres across, which on its own
+ * would fill the screen with the roof.
+ */
+const PLACE_MAX_ZOOM = 17;
+
+/** The room kept around a framed place, so its pin and name do not sit against the map's edge. */
+const PLACE_PADDING = 60;
+
/** The zoom level a marker is shown at, closer in since a note marks a spot rather than an area. */
const MARKER_ZOOM = 15;
@@ -47,8 +56,11 @@ type SearchEntry = {
icon: string;
} & (
| { kind: "marker"; center: [number, number] }
- /** A place from the geocoder, `name` being what its pin is labelled with. */
- | { kind: "place"; center: [number, number]; name: string }
+ /**
+ * A place from the geocoder: `name` is what its pin is labelled with, and `bounds` how much
+ * ground it covers, where the geocoder says.
+ */
+ | { kind: "place"; center: [number, number]; name: string; bounds?: GeoSearchResult["bounds"] }
/** Runs the geocoder for `query`. */
| { kind: "geocode"; query: string }
/** Reports on a geocoder run; picking it does nothing. */
@@ -139,7 +151,13 @@ export default function SearchBox({ notes, isDarkTheme }: SearchBoxProps) {
setDismissed(true);
// A note already has a marker of its own to fly to; only a place needs one put down.
setPickedPlace(entry.kind === "place" ? { center: entry.center, name: entry.name } : undefined);
- map.flyTo({ center: entry.center, zoom: entry.kind === "marker" ? MARKER_ZOOM : PLACE_ZOOM });
+ if (entry.kind === "place" && entry.bounds) {
+ // Framed by what the place covers rather than flown to at a level guessed for every
+ // place alike: one zoom that suits a city shows a street as the city around it.
+ map.fitBounds(entry.bounds, { padding: PLACE_PADDING, maxZoom: PLACE_MAX_ZOOM });
+ } else {
+ map.flyTo({ center: entry.center, zoom: entry.kind === "marker" ? MARKER_ZOOM : PLACE_ZOOM });
+ }
}
}, [ map, runGeocoder ]);
@@ -240,7 +258,8 @@ function placeEntry(result: GeoSearchResult): SearchEntry {
label: result.label,
name: result.name,
icon: "bx bx-map-pin",
- center: [ result.lng, result.lat ]
+ center: [ result.lng, result.lat ],
+ bounds: result.bounds
};
}
diff --git a/apps/client/src/widgets/collections/geomap/geocoding.ts b/apps/client/src/widgets/collections/geomap/geocoding.ts
index ee3cf55380..def28dd194 100644
--- a/apps/client/src/widgets/collections/geomap/geocoding.ts
+++ b/apps/client/src/widgets/collections/geomap/geocoding.ts
@@ -10,6 +10,12 @@ export interface GeoSearchResult {
name: string;
lat: number;
lng: number;
+ /**
+ * What the place covers, as `[[west, south], [east, north]]`, where the geocoder says. A street
+ * and the city around it are told apart by their extent rather than by one zoom level guessed
+ * for both.
+ */
+ bounds?: [[number, number], [number, number]];
}
export interface GeocodingProvider {
diff --git a/apps/client/src/widgets/collections/geomap/nominatim.spec.ts b/apps/client/src/widgets/collections/geomap/nominatim.spec.ts
index 8adf77610d..8663a152d3 100644
--- a/apps/client/src/widgets/collections/geomap/nominatim.spec.ts
+++ b/apps/client/src/widgets/collections/geomap/nominatim.spec.ts
@@ -14,6 +14,7 @@ function place(overrides: Record<string, unknown> = {}) {
place_id: 240109189,
name: "Berlin",
display_name: "Berlin, Germany",
+ boundingbox: [ "52.3382448", "52.6755087", "13.0883450", "13.7611609" ],
lat: "52.5170365",
lon: "13.3888599",
...overrides
@@ -68,13 +69,21 @@ describe("Nominatim geocoding", () => {
place(),
place({ place_id: 2, display_name: "Nowhere", lat: "not a number" }),
place({ place_id: 3, display_name: undefined }),
- place({ place_id: 4, name: undefined, display_name: "Paris, France", lat: "48.8566", lon: "2.3522" })
+ place({
+ place_id: 4, name: undefined, display_name: "Paris, France", lat: "48.8566", lon: "2.3522",
+ boundingbox: [ "not", "a", "box", "at all" ]
+ })
]);
expect(await search("anywhere")).toEqual([
- { id: "240109189", label: "Berlin, Germany", name: "Berlin", lat: 52.5170365, lng: 13.3888599 },
- // Named by the leading part of its label, Nominatim having named it nothing itself.
- { id: "4", label: "Paris, France", name: "Paris", lat: 48.8566, lng: 2.3522 }
+ {
+ id: "240109189", label: "Berlin, Germany", name: "Berlin", lat: 52.5170365, lng: 13.3888599,
+ // Written south-north-west-east by Nominatim, and read as MapLibre frames one.
+ bounds: [ [ 13.088345, 52.3382448 ], [ 13.7611609, 52.6755087 ] ]
+ },
+ // Named by the leading part of its label, Nominatim having named it nothing itself, and
+ // framed by nothing, its extent being unreadable.
+ { id: "4", label: "Paris, France", name: "Paris", lat: 48.8566, lng: 2.3522, bounds: undefined }
]);
});
diff --git a/apps/client/src/widgets/collections/geomap/nominatim.ts b/apps/client/src/widgets/collections/geomap/nominatim.ts
index 0d8cb0928d..bcc44b1e03 100644
--- a/apps/client/src/widgets/collections/geomap/nominatim.ts
+++ b/apps/client/src/widgets/collections/geomap/nominatim.ts
@@ -62,6 +62,8 @@ interface NominatimPlace {
name?: string;
lat?: string;
lon?: string;
+ /** `[south, north, west, east]`, as strings, which is the order Nominatim writes it in. */
+ boundingbox?: string[];
}
/** A place as the app holds one, or `null` where the entry carries no readable position. */
@@ -79,10 +81,31 @@ function toSearchResult(place: NominatimPlace): GeoSearchResult | null {
// where the leading part of the label is what names it.
name: place.name || place.display_name.split(",")[0].trim(),
lat,
- lng
+ lng,
+ bounds: toBounds(place.boundingbox)
};
}
+/** The extent Nominatim reports, as MapLibre reads one, or nothing where it cannot be read. */
+function toBounds(boundingbox: string[] | undefined): GeoSearchResult["bounds"] {
+ if (boundingbox?.length !== 4) {
+ return undefined;
+ }
+
+ const [ south, north, west, east ] = boundingbox.map(Number);
+ if (![ south, north, west, east ].every(Number.isFinite)) {
+ return undefined;
+ }
+
+ // A place spanning the antimeridian is reported west of east, which frames the whole world the
+ // long way round. Rare enough to be left to the fallback zoom rather than split in two.
+ if (west > east) {
+ return undefined;
+ }
+
+ return [ [ west, south ], [ east, north ] ];
+}
+
/**
* Holds a request back until the interval the policy asks for has passed since the last one.
*