Skip to content

Commit

Permalink
fix: allow geocoding entire addresses that don't match address points
Browse files Browse the repository at this point in the history
Fixes #707
  • Loading branch information
stdavis committed Nov 18, 2024
1 parent e50760f commit 93f95b0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/components/search-wizard/filters/StreetAddress.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default function StreetAddress({ send }) {
3857,
);
setSherlockConfig({
placeHolder: 'search by street address...',
placeHolder: 'street address, city or zip',
onSherlockMatch: (features) => setAddress(features[0]),
provider,
maxResultsToDisplay: 10,
Expand All @@ -41,7 +41,7 @@ export default function StreetAddress({ send }) {
return (
<>
<Buffer onChange={setBufferGeometry} inputGeometry={address?.geometry} />
{sherlockConfig && <Sherlock {...sherlockConfig} className="mt-2" />}
{sherlockConfig && <Sherlock {...sherlockConfig} />}
{/* buffer to make sure user can scroll far enough to see the entire select filter type dropdown */}
<div className="h-28" />
</>
Expand Down
46 changes: 38 additions & 8 deletions src/utah-design-system/Sherlock.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,19 @@ export default function Sherlock({

const searchValue = getSearchValue(selectedItem);

let contextValue;
if (provider.contextField) {
contextValue = selectedItem.attributes[provider.contextField];
}
let results;
if (searchValue) {
let contextValue;
if (provider.contextField) {
contextValue = selectedItem.attributes[provider.contextField];
}

const response = await provider.getFeature(searchValue, contextValue);
const response = await provider.getFeature(searchValue, contextValue);

const results = response.items;
results = response.items;
} else {
results = [selectedItem];
}

const graphics = results.map(
(result) =>
Expand Down Expand Up @@ -213,7 +218,7 @@ export default function Sherlock({

const getMenuItems = () => {
const commonClasses =
'rounded-md border border-slate-400 rounded-md px-2 py-1';
'rounded-md border border-slate-400 rounded-md px-2 py-1 cursor-pointer';
if (state.short) {
return (
// primary
Expand Down Expand Up @@ -254,7 +259,7 @@ export default function Sherlock({
})}
>
<Highlighted
text={item.attributes[provider.searchField]}
text={item.attributes[provider.searchField] || item.address}
highlight={inputValue}
></Highlighted>
<span className="text-right text-sm">
Expand Down Expand Up @@ -646,6 +651,31 @@ export class LocatorSuggestProvider extends ProviderBase {
}

async search(searchString, maxResults) {
if (searchString.match(',')) {
const findCandidatesUrl = `${this.url}/findAddressCandidates`;
const responseJson = await ky(findCandidatesUrl, {
searchParams: {
'Single Line Input': searchString,
outSR: `{"wkid":${this.outSRID}}`,
},
}).json();

if (responseJson.candidates.length === 0) {
return { items: [] };
}

const candidate = responseJson.candidates[0];
candidate.geometry = {
...candidate.location,
type: 'point',
spatialReference: {
wkid: this.outSRID,
},
};

return { items: [candidate] };
}

const suggestUrl = `${
this.url
}/suggest?text=${searchString}&maxSuggestions=${maxResults || 10}`;
Expand Down

0 comments on commit 93f95b0

Please sign in to comment.