diff --git a/components/src/maplibre/Geocoder/Geocoder.svelte b/components/src/maplibre/Geocoder/Geocoder.svelte index 3c2d0694..40514a94 100644 --- a/components/src/maplibre/Geocoder/Geocoder.svelte +++ b/components/src/maplibre/Geocoder/Geocoder.svelte @@ -16,6 +16,7 @@ languages = 'en', types = [], bbox, + proximity, placeholder, limit = 3, size = 'default', @@ -35,7 +36,9 @@ language: languagesArr.join(','), countries: countriesArr.join(','), types: typesArr.join(','), - bbox: bbox, + bbox, + trackProximity: !proximity, + proximity, showResultsWhileTyping: true, showResultMarkers: false, debounceSearch: 25, diff --git a/components/src/maplibre/Geocoder/GeocoderAPIs.ts b/components/src/maplibre/Geocoder/GeocoderAPIs.ts index de565fc0..20dc77e7 100644 --- a/components/src/maplibre/Geocoder/GeocoderAPIs.ts +++ b/components/src/maplibre/Geocoder/GeocoderAPIs.ts @@ -17,6 +17,7 @@ export class MaptilerGeocoderAPI implements MaplibreGeocoderApi { type: 'FeatureCollection', features: [] }; + try { const params = new URLSearchParams( Object.fromEntries( @@ -24,6 +25,7 @@ export class MaptilerGeocoderAPI implements MaplibreGeocoderApi { country: config.countries || '', language: config.language || '', types: config.types || '', + proximity: config.proximity?.join(',') || '', bbox: config.bbox?.join(',') || '', key: this.key || '' }).filter(([_, value]) => { diff --git a/components/src/maplibre/Geocoder/GeocoderProps.ts b/components/src/maplibre/Geocoder/GeocoderProps.ts index 1592cc19..b2b430e3 100644 --- a/components/src/maplibre/Geocoder/GeocoderProps.ts +++ b/components/src/maplibre/Geocoder/GeocoderProps.ts @@ -29,6 +29,10 @@ export default interface GeocoderProps { * Limit search to a [w, s, e, n] bounding box */ bbox?: number[]; + /** + * A coordinate pair to bias search results + */ + proximity?: { longitude: number; latitude: number }; /** * Limit search to one or more languages. The UI is localised to the first language specified if [available](https://github.com/maplibre/maplibre-gl-geocoder/blob/main/lib/localization.ts). */ diff --git a/components/src/maplibre/GeocoderControl/GeocoderAPIs.ts b/components/src/maplibre/GeocoderControl/GeocoderAPIs.ts deleted file mode 100644 index de565fc0..00000000 --- a/components/src/maplibre/GeocoderControl/GeocoderAPIs.ts +++ /dev/null @@ -1,63 +0,0 @@ -import { - type CarmenGeojsonFeature, - type MaplibreGeocoderApi, - type MaplibreGeocoderApiConfig, - type MaplibreGeocoderFeatureResults -} from '@maplibre/maplibre-gl-geocoder'; - -export class MaptilerGeocoderAPI implements MaplibreGeocoderApi { - key: string; - - constructor(key: string) { - this.key = key; - } - - async forwardGeocode(config: MaplibreGeocoderApiConfig): Promise { - const result: MaplibreGeocoderFeatureResults = { - type: 'FeatureCollection', - features: [] - }; - try { - const params = new URLSearchParams( - Object.fromEntries( - Object.entries({ - country: config.countries || '', - language: config.language || '', - types: config.types || '', - bbox: config.bbox?.join(',') || '', - key: this.key || '' - }).filter(([_, value]) => { - return value !== ''; - }) - ) - ); - - const response = await fetch( - `https://api.maptiler.com/geocoding/${config.query}.json?${params.toString()}` - ); - const geojson = await response.json(); - for (const feature of geojson.features) { - const res: CarmenGeojsonFeature = { - type: 'Feature', - id: feature.id, - place_type: ['place'], - place_name: feature.place_name, - language: feature.properties.display_name, - text: feature.text, - bbox: feature.bbox, - geometry: { - type: 'Point', - coordinates: feature.center - }, - properties: { - text: feature.text - } - }; - result.features.push(res); - } - } catch (e) { - console.error(`Failed to forwardGeocode with error: ${e}`); - } - return result; - } -} diff --git a/components/src/maplibre/GeocoderControl/GeocoderControl.stories.svelte b/components/src/maplibre/GeocoderControl/GeocoderControl.stories.svelte index b127cc63..1b63d462 100644 --- a/components/src/maplibre/GeocoderControl/GeocoderControl.stories.svelte +++ b/components/src/maplibre/GeocoderControl/GeocoderControl.stories.svelte @@ -131,6 +131,22 @@ + + +
+ + + +
+
+