From 14956e229ced18dbd56c2cbc80ce95db42ec2e36 Mon Sep 17 00:00:00 2001 From: pietervdvn Date: Thu, 28 Apr 2022 00:28:04 +0200 Subject: [PATCH] Small refactoring of search --- Logic/Osm/Geocoding.ts | 21 +++++++-------- UI/BigComponents/SearchAndGo.ts | 47 ++++++++++++++++----------------- 2 files changed, 33 insertions(+), 35 deletions(-) diff --git a/Logic/Osm/Geocoding.ts b/Logic/Osm/Geocoding.ts index 55dd99681..7f8749c50 100644 --- a/Logic/Osm/Geocoding.ts +++ b/Logic/Osm/Geocoding.ts @@ -1,23 +1,22 @@ import State from "../../State"; import {Utils} from "../../Utils"; +import {BBox} from "../BBox"; + +export interface GeoCodeResult { + display_name: string, + lat: number, lon: number, boundingbox: number[], + osm_type: string, osm_id: string +} export class Geocoding { private static readonly host = "https://nominatim.openstreetmap.org/search?"; - static Search(query: string, - handleResult: ((places: { - display_name: string, lat: number, lon: number, boundingbox: number[], - osm_type: string, osm_id: string - }[]) => void), - onFail: (() => void)) { - const b = State.state.currentBounds.data; + static async Search(query: string): Promise { + const b = State?.state?.currentBounds?.data ?? BBox.global; const url = Geocoding.host + "format=json&limit=1&viewbox=" + `${b.getEast()},${b.getNorth()},${b.getWest()},${b.getSouth()}` + "&accept-language=nl&q=" + query; - Utils.downloadJson( - url) - .then(handleResult) - .catch(onFail); + return Utils.downloadJson(url) } } diff --git a/UI/BigComponents/SearchAndGo.ts b/UI/BigComponents/SearchAndGo.ts index a0cb766bd..44efc2d10 100644 --- a/UI/BigComponents/SearchAndGo.ts +++ b/UI/BigComponents/SearchAndGo.ts @@ -44,38 +44,37 @@ export default class SearchAndGo extends Combine { ); // Triggered by 'enter' or onclick - function runSearch() { + async function runSearch() { const searchString = searchField.GetValue().data; if (searchString === undefined || searchString === "") { return; } searchField.GetValue().setData(""); placeholder.setData(Translations.t.general.search.searching); - Geocoding.Search( - searchString, - (result) => { - console.log("Search result", result); - if (result.length == 0) { - placeholder.setData(Translations.t.general.search.nothing); - return; - } + try { - const poi = result[0]; - const bb = poi.boundingbox; - const bounds: [[number, number], [number, number]] = [ - [bb[0], bb[2]], - [bb[1], bb[3]], - ]; - state.selectedElement.setData(undefined); - Hash.hash.setData(poi.osm_type + "/" + poi.osm_id); - state.leafletMap.data.fitBounds(bounds); - placeholder.setData(Translations.t.general.search.search); - }, - () => { - searchField.GetValue().setData(""); - placeholder.setData(Translations.t.general.search.error); + const result = await Geocoding.Search(searchString); + + console.log("Search result", result); + if (result.length == 0) { + placeholder.setData(Translations.t.general.search.nothing); + return; } - ); + + const poi = result[0]; + const bb = poi.boundingbox; + const bounds: [[number, number], [number, number]] = [ + [bb[0], bb[2]], + [bb[1], bb[3]], + ]; + state.selectedElement.setData(undefined); + Hash.hash.setData(poi.osm_type + "/" + poi.osm_id); + state.leafletMap.data.fitBounds(bounds); + placeholder.setData(Translations.t.general.search.search) + }catch(e){ + searchField.GetValue().setData(""); + placeholder.setData(Translations.t.general.search.error); + } } searchField.enterPressed.addCallback(runSearch);