updates on the address field on 7/8/2026
This commit is contained in:
@@ -143,22 +143,45 @@ const buildParams = (query, bias) => {
|
||||
return params;
|
||||
};
|
||||
|
||||
const searchOnce = async (query, bias) => {
|
||||
const params = buildParams(query, bias);
|
||||
params.set('limit', '1');
|
||||
const res = await fetchWithTimeout(`${NOMINATIM_SEARCH_URL}?${params.toString()}`, { headers: { Accept: 'application/json' } });
|
||||
if (!res.ok) return null;
|
||||
const results = await res.json();
|
||||
return results?.length ? results[0] : null;
|
||||
};
|
||||
|
||||
// Standalone forward-geocode helper — replaces `Geocode.fromAddress(address)`
|
||||
// for call sites that resolve a plain text address without a predictions
|
||||
// dropdown (e.g. re-geocoding on submit). Returns a `toPlace`-shaped object,
|
||||
// or null if nothing matched.
|
||||
//
|
||||
// Unlike Google's geocoder, Nominatim only matches when every term in the
|
||||
// query belongs to the same indexed record — it has no "closest approximate
|
||||
// match" behaviour. A door number + unlisted micro-locality/street name (very
|
||||
// common for Indian addresses, e.g. "60, Keezha Raman Puthoor, ...") makes
|
||||
// the whole query match nothing, even though the city/postcode later in the
|
||||
// string resolves fine on its own. So on a miss, retry with the leading
|
||||
// (most specific, least likely to be indexed) comma-separated segments
|
||||
// dropped one at a time until something matches — worst case this lands on
|
||||
// just "<city> <postcode>", an approximate pin instead of no pin at all.
|
||||
export async function geocodeAddress(address, { bias } = {}) {
|
||||
if (!address) return null;
|
||||
const plusCodeResult = await plusCodeToResult(address, bias);
|
||||
if (plusCodeResult) return toPlace(plusCodeResult);
|
||||
const segments = address
|
||||
.split(',')
|
||||
.map((s) => s.trim())
|
||||
.filter(Boolean);
|
||||
const queries = segments.length > 1 ? segments.map((_, i) => segments.slice(i).join(', ')) : [address];
|
||||
try {
|
||||
const params = buildParams(address, bias);
|
||||
params.set('limit', '1');
|
||||
const res = await fetchWithTimeout(`${NOMINATIM_SEARCH_URL}?${params.toString()}`, { headers: { Accept: 'application/json' } });
|
||||
if (!res.ok) return null;
|
||||
const results = await res.json();
|
||||
if (!results?.length) return null;
|
||||
return toPlace(results[0]);
|
||||
for (const query of queries) {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
const result = await searchOnce(query, bias);
|
||||
if (result) return toPlace(result);
|
||||
}
|
||||
return null;
|
||||
} catch (err) {
|
||||
console.error('geocodeAddress error:', err);
|
||||
return null;
|
||||
|
||||
Reference in New Issue
Block a user