forked from MapComplete/MapComplete
		
	Small refactoring of search
This commit is contained in:
		
							parent
							
								
									fc3a043795
								
							
						
					
					
						commit
						14956e229c
					
				
					 2 changed files with 33 additions and 35 deletions
				
			
		|  | @ -1,23 +1,22 @@ | ||||||
| import State from "../../State"; | import State from "../../State"; | ||||||
| import {Utils} from "../../Utils"; | 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 { | export class Geocoding { | ||||||
| 
 | 
 | ||||||
|     private static readonly host = "https://nominatim.openstreetmap.org/search?"; |     private static readonly host = "https://nominatim.openstreetmap.org/search?"; | ||||||
| 
 | 
 | ||||||
|     static Search(query: string, |     static async Search(query: string): Promise<GeoCodeResult[]> { | ||||||
|                   handleResult: ((places: { |         const b = State?.state?.currentBounds?.data ?? BBox.global; | ||||||
|                       display_name: string, lat: number, lon: number, boundingbox: number[], |  | ||||||
|                       osm_type: string, osm_id: string |  | ||||||
|                   }[]) => void), |  | ||||||
|                   onFail: (() => void)) { |  | ||||||
|         const b = State.state.currentBounds.data; |  | ||||||
|         const url = Geocoding.host + "format=json&limit=1&viewbox=" + |         const url = Geocoding.host + "format=json&limit=1&viewbox=" + | ||||||
|             `${b.getEast()},${b.getNorth()},${b.getWest()},${b.getSouth()}` + |             `${b.getEast()},${b.getNorth()},${b.getWest()},${b.getSouth()}` + | ||||||
|             "&accept-language=nl&q=" + query; |             "&accept-language=nl&q=" + query; | ||||||
|         Utils.downloadJson( |        return Utils.downloadJson(url) | ||||||
|             url) |  | ||||||
|             .then(handleResult) |  | ||||||
|             .catch(onFail); |  | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -44,16 +44,17 @@ export default class SearchAndGo extends Combine { | ||||||
|         ); |         ); | ||||||
| 
 | 
 | ||||||
|         // Triggered by 'enter' or onclick
 |         // Triggered by 'enter' or onclick
 | ||||||
|         function runSearch() { |         async function runSearch() { | ||||||
|             const searchString = searchField.GetValue().data; |             const searchString = searchField.GetValue().data; | ||||||
|             if (searchString === undefined || searchString === "") { |             if (searchString === undefined || searchString === "") { | ||||||
|                 return; |                 return; | ||||||
|             } |             } | ||||||
|             searchField.GetValue().setData(""); |             searchField.GetValue().setData(""); | ||||||
|             placeholder.setData(Translations.t.general.search.searching); |             placeholder.setData(Translations.t.general.search.searching); | ||||||
|             Geocoding.Search( |             try { | ||||||
|                 searchString, | 
 | ||||||
|                 (result) => { |                 const result = await Geocoding.Search(searchString); | ||||||
|  | 
 | ||||||
|                 console.log("Search result", result); |                 console.log("Search result", result); | ||||||
|                 if (result.length == 0) { |                 if (result.length == 0) { | ||||||
|                     placeholder.setData(Translations.t.general.search.nothing); |                     placeholder.setData(Translations.t.general.search.nothing); | ||||||
|  | @ -69,13 +70,11 @@ export default class SearchAndGo extends Combine { | ||||||
|                 state.selectedElement.setData(undefined); |                 state.selectedElement.setData(undefined); | ||||||
|                 Hash.hash.setData(poi.osm_type + "/" + poi.osm_id); |                 Hash.hash.setData(poi.osm_type + "/" + poi.osm_id); | ||||||
|                 state.leafletMap.data.fitBounds(bounds); |                 state.leafletMap.data.fitBounds(bounds); | ||||||
|                     placeholder.setData(Translations.t.general.search.search); |                 placeholder.setData(Translations.t.general.search.search) | ||||||
|                 }, |             }catch(e){ | ||||||
|                 () => { |  | ||||||
|                 searchField.GetValue().setData(""); |                 searchField.GetValue().setData(""); | ||||||
|                 placeholder.setData(Translations.t.general.search.error); |                 placeholder.setData(Translations.t.general.search.error); | ||||||
|             } |             } | ||||||
|             ); |  | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         searchField.enterPressed.addCallback(runSearch); |         searchField.enterPressed.addCallback(runSearch); | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue