Prompt
How do I add autocomplete for address search?
Latest observation
Oct 15, 2026
OpenAI APIWeb search: off
To add autocomplete for address search, you typically use an address/place autocomplete API from a mapping provider, then show suggestions as the user types.
Common options
- Google Places Autocomplete
- Mapbox Search / Geocoding
- HERE Geocoding & Search
- Algolia Places (less common now)
- OpenStreetMap/Nominatim via your own service or a wrapper
Basic flow
- User types into an input field.
- After a few characters, send the query to the autocomplete API.
- Display returned suggestions in a dropdown.
- When the user selects one, fetch full address details if needed.
- Fill your form with the chosen address.
Example with Google Places Autocomplete
Frontend
<input id="address" type="text" placeholder="Enter address" />
<div id="suggestions"></div>
<script>
let timeout;
document.getElementById('address').addEventListener('input', (e) => {
clearTimeout(timeout);
const query = e.target.value;
if (query.length < 3) return;
timeout = setTimeout(async () => {
const res = await fetch(`/api/address-autocomplete?q=${encodeURIComponent(query)}`);
const suggestions = await res.json();
document.getElementById('suggestions').innerHTML = suggestions
.map(item => `<div class="suggestion" data-id="${item.id}">${item.text}</div>`)
.join('');
}, 300);
});
</script>
Backend example
app.get('/api/address-autocomplete', async (req, res) => {
const q = req.query.q;
const apiRes = await fetch(
`https://maps.googleapis.com/maps/api/place/autocomplete/json?input=${encodeURIComponent(q)}&key=${process.env.GOOGLE_MAPS_API_KEY}`
);
const data = await apiRes.json();
res.json(
data.predictions.map(p => ({
id: p.place_id,
text: p.description
}))
);
});
Important tips
- Debounce requests to avoid hitting rate limits.
- Limit input length before querying, e.g. 3+ characters.
- Use session tokens if the provider recommends them.
- Handle selection separately from typed text.
- Secure your API key by proxying requests through your backend when needed.
- Bias results by country or map bounds if relevant.
If you want a quick no-code-ish option
If you’re using a framework like React, many libraries already wrap this:
@react-google-maps/apireact-places-autocomplete- Mapbox autocomplete components
If you tell me your stack, I can show you a ready-to-use example for React, Vue, Angular, plain JS, iOS, or Android.