Prompt
How do I build nearby place search with autocomplete in a web form?
Latest observation
Oct 15, 2026
OpenAI APIWeb search: off
A common way to build this is:
- Autocomplete the user’s place/location input
- Use the selected place to get coordinates
- Search nearby places from those coordinates
- Show results in the form or a results panel
There are two main approaches:
1) Use a maps/places API
This is the simplest and most reliable option.
Typical flow
- User types in an address, city, or landmark
- Your app queries an autocomplete API
- User picks a suggestion
- You store:
- place name
- place ID
- latitude
- longitude
- Then you query a nearby search API with:
- coordinates
- radius
- category/type filter
Popular APIs
- Google Maps Places API
- Mapbox Search API
- TomTom Search API
- OpenStreetMap + Nominatim for geocoding, though autocomplete/nearby place search is more limited unless you combine services
2) Build it yourself with geocoding + your own nearby database
If you already have a database of places:
- Autocomplete against your own indexed place table
- Geocode the selected location to coordinates
- Query places nearby using distance calculations
This is useful for:
- Store locators
- Internal business directories
- Large custom datasets
Recommended architecture
Frontend
- Input field with autocomplete dropdown
- Debounce typing to avoid too many API calls
- On selection, save coordinates
- Trigger nearby search on selection or on submit
Backend
- Optional proxy for API calls
- Store API keys securely
- Cache frequent autocomplete and nearby queries
- Enforce rate limits
Example UX
Step 1: Location input
"Enter a city or address"
Step 2: Autocomplete suggestions
New York, NY
Newark, NJ
Newport, RI
...
Step 3: User selects one
You store:
{
"place_id": "abc123",
"name": "New York, NY",
"lat": 40.7128,
"lng": -74.0060
}
Step 4: Nearby search
Query:
- radius: 5000 meters
- type: restaurants
Return nearby places:
[
{ "name": "Joe's Pizza", "distance_m": 320 },
{ "name": "Shake Shack", "distance_m": 860 }
]
Example using Google Places API
Autocomplete request
You send the typed text to the autocomplete endpoint.
Then when the user selects a suggestion:
- use the place ID to fetch place details
- get lat/lng
Nearby search request
Use the location from the selected place and call nearby search.
Typical parameters
location=lat,lngradius=5000type=restaurant
Frontend pattern
Debounced autocomplete
Use a delay of about 200–400 ms between keystrokes and API calls.
Pseudocode
let timeout;
input.addEventListener("input", (e) => {
clearTimeout(timeout);
timeout = setTimeout(() => {
fetchSuggestions(e.target.value);
}, 300);
});
Nearby search example logic
async function onPlaceSelected(place) {
const { lat, lng } = place.geometry.location;
const res = await fetch(`/api/nearby?lat=${lat}&lng=${lng}&radius=5000&type=restaurant`);
const results = await res.json();
renderResults(results);
}
Important implementation details
1. Use session tokens for autocomplete
Some APIs charge per session or use session-based billing. Group autocomplete keystrokes into one session.
2. Don’t expose API keys in the browser
Use a backend proxy when possible.
3. Cache results
Autocomplete often repeats queries like “San”, “San F”, “San Fr…”.
4. Handle location permission carefully
If you also want “near me” behavior:
- ask for browser geolocation permission
- fall back to typed location if denied
5. Allow radius and category filters
For example:
- 1 km restaurants
- 10 km gas stations
- cafes open now
If you want “near me” search too
You can combine:
- browser geolocation to get current user location
- autocomplete for manual location entry
Flow:
- If location permission granted:
- use current coordinates for nearby search
- Else:
- ask user to type a place and use autocomplete
Minimal implementation plan
- Add an autocomplete text input
- Call place autocomplete API as the user types
- Let the user select a suggestion
- Get lat/lng for that selection
- Call nearby search API using those coordinates
- Render results
- Add debouncing, caching, and error handling
If you’re using a framework
React
- controlled input
- autocomplete dropdown component
useEffectfor debounced fetches
Vue
- reactive input
- watch the query
- fetch suggestions with debounce
Plain JS
- input event listener
- dropdown list below input
- click handlers for selection
Example UI components
- Location input
- Suggestions dropdown
- Radius selector
- Category dropdown
- “Use my location” button
- Results list/map
If you want, I can give you:
- a plain HTML/JS example,
- a React example, or
- a Google Maps or Mapbox-specific implementation.