@@ -5,7 +5,7 @@ import { stringify } from "querystring";
55// eslint-disable-next-line prettier/prettier
66import type { LonLatOutput } from "@conveyal/lonlat"
77import type { AutocompleteQuery , ReverseQuery , SearchQuery } from "../../geocoders/types"
8- import type { HereResponse } from "./types" ;
8+ import type { Boundary , HereResponse , Item } from "./types" ;
99
1010const AUTOCOMPLETE_URL =
1111 "https://autosuggest.search.hereapi.com/v1/autosuggest" ;
@@ -41,6 +41,12 @@ function run({ options, query, url }: HereFetchArgs): Promise<HereResponse> {
4141 return fetch ( `${ url } ?${ stringify ( query ) } ` , options ) . then ( res => res . json ( ) ) ;
4242}
4343
44+ const checkItemInBoundary = ( { rect } : Boundary ) => ( { position } : Item ) => {
45+ const { maxLat, maxLon, minLat, minLon } = rect
46+ const { lat, lng } = position
47+ return lng <= maxLon && lng >= minLon && lat <= maxLat && lat >= minLat
48+ }
49+
4450/**
4551 * Search for an address using
4652 * Here's {@link https://developer.here.com/documentation/geocoding-search-api/api-reference-swagger.html|Autocomplete}
@@ -55,7 +61,7 @@ function run({ options, query, url }: HereFetchArgs): Promise<HereResponse> {
5561 * @param {string } $0.text query text
5662 * @return {Promise } A Promise that'll get resolved with the autocomplete result
5763 */
58- function autocomplete ( {
64+ async function autocomplete ( {
5965 apiKey,
6066 boundary,
6167 focusPoint,
@@ -66,6 +72,21 @@ function autocomplete({
6672 // build query
6773 const query : HereQuery = { apiKey, limit : size , q : text , show : "details" } ;
6874
75+ if ( focusPoint ) {
76+ const { lat, lon } : LonLatOutput = normalize ( focusPoint ) ;
77+ query . at = `${ lat } ,${ lon } ` ;
78+ const res = await run ( {
79+ options,
80+ query,
81+ url : AUTOCOMPLETE_URL
82+ } ) ;
83+ if ( boundary ?. rect ) {
84+ // HERE does not support a boundary when you use a focus point
85+ // This workaround filters the results internally to the boundary
86+ res . items = res . items . filter ( checkItemInBoundary ( boundary ) )
87+ }
88+ return res
89+ }
6990 if ( boundary ) {
7091 const { country, rect } = boundary ;
7192 if ( country ) query . in = `countryCode:${ country } ` ;
@@ -77,12 +98,8 @@ function autocomplete({
7798 rect . maxLat
7899 ] . join ( "," ) } `;
79100 }
80- } else if ( focusPoint ) {
81- // Only add focusPoint if the boundary is not specified.
82- // HERE only supports one or the other, not both.
83- const { lat, lon } : LonLatOutput = normalize ( focusPoint ) ;
84- query . at = `${ lat } ,${ lon } ` ;
85101 }
102+
86103 return run ( {
87104 options,
88105 query,
0 commit comments