Search: add two global search options

This commit is contained in:
Pieter Vander Vennet 2024-10-17 21:56:31 +02:00
parent 0a0d0a15cb
commit 181219928c
2 changed files with 11 additions and 7 deletions

View file

@ -13,18 +13,20 @@ import { GeoOperations } from "../GeoOperations"
import { Store, Stores } from "../UIEventSource" import { Store, Stores } from "../UIEventSource"
export default class PhotonSearch implements GeocodingProvider, ReverseGeocodingProvider { export default class PhotonSearch implements GeocodingProvider, ReverseGeocodingProvider {
private _endpoint: string private readonly _endpoint: string
private supportedLanguages = ["en", "de", "fr"] private supportedLanguages = ["en", "de", "fr"]
private static readonly types = { private static readonly types = {
"R": "relation", "R": "relation",
"W": "way", "W": "way",
"N": "node", "N": "node",
} }
private readonly ignoreBounds: boolean
private readonly suggestionLimit: number = 5 private readonly suggestionLimit: number = 5
private readonly searchLimit: number = 1 private readonly searchLimit: number = 1
constructor(suggestionLimit:number = 5, searchLimit:number = 1, endpoint?: string) { constructor(ignoreBounds: boolean = false, suggestionLimit:number = 5, searchLimit:number = 1, endpoint?: string) {
this.ignoreBounds = ignoreBounds
this.suggestionLimit = suggestionLimit this.suggestionLimit = suggestionLimit
this.searchLimit = searchLimit this.searchLimit = searchLimit
this._endpoint = endpoint ?? Constants.photonEndpoint ?? "https://photon.komoot.io/" this._endpoint = endpoint ?? Constants.photonEndpoint ?? "https://photon.komoot.io/"
@ -59,7 +61,7 @@ export default class PhotonSearch implements GeocodingProvider, ReverseGeocoding
} }
suggest(query: string, options?: GeocodingOptions): Store<GeocodeResult[]> { suggest(query: string, options?: GeocodingOptions): Store<GeocodeResult[]> {
return Stores.FromPromise(this.search(query, options, this.suggestionLimit)) return Stores.FromPromise(this.search(query, options))
} }
private buildDescription(entry: Feature) { private buildDescription(entry: Feature) {
@ -111,13 +113,13 @@ export default class PhotonSearch implements GeocodingProvider, ReverseGeocoding
return p.type return p.type
} }
async search(query: string, options?: GeocodingOptions, limit?: number): Promise<GeocodeResult[]> { async search(query: string, options?: GeocodingOptions): Promise<GeocodeResult[]> {
if (query.length < 3) { if (query.length < 3) {
return [] return []
} }
limit ??= this.searchLimit const limit = this.searchLimit
let bbox = "" let bbox = ""
if (options?.bbox) { if (options?.bbox && !this.ignoreBounds) {
const [lon, lat] = options.bbox.center() const [lon, lat] = options.bbox.center()
bbox = `&lon=${lon}&lat=${lat}` bbox = `&lon=${lon}&lat=${lat}`
} }

View file

@ -39,7 +39,9 @@ export default class SearchState {
new LocalElementSearch(state, 5), new LocalElementSearch(state, 5),
new CoordinateSearch(), new CoordinateSearch(),
new OpenStreetMapIdSearch(state), new OpenStreetMapIdSearch(state),
new PhotonSearch() // new NominatimGeocoding(), new PhotonSearch(true, 2),
new PhotonSearch(),
// new NominatimGeocoding(),
] ]
const bounds = state.mapProperties.bounds const bounds = state.mapProperties.bounds