Prompt
How do I build a nearby store search with autocomplete and place lookup?
Latest observation
A good pattern is to split it into three parts:
- Autocomplete while the user types
- Place lookup/details after they pick a suggestion
- Nearby store search using the selected place/location
1) Autocomplete
Use a places/autocomplete API to return matching locations as the user types.
Typical flow:
- User types:
"San Fra" - Frontend sends query to backend or directly to a maps provider autocomplete endpoint
- Show a dropdown of suggestions
- Each suggestion should include:
- display text
- unique place ID
- maybe short address / category
Important
Don’t use free-text search results alone for this step; use place IDs so the selected location is precise.
2) Place lookup
When the user clicks a suggestion, fetch the full place details:
- coordinates (
lat,lng) - formatted address
- city / region / country
- viewport/bounds if available
This gives you a reliable center point for the nearby store search.
3) Nearby store search
Now search for stores around the selected coordinates.
Common options:
- Geo-indexed database: best if you control store data
- Maps Places Nearby Search API: best if you want third-party POIs
- Search engine + geospatial filter: if you have many stores and search fields
If you manage the store list
Store records should include:
idnamelocation: { lat, lng }- optional tags: category, hours, rating, inventory, etc.
Then query by radius:
- 1 km, 5 km, etc.
- rank by distance
- optionally filter by open now, category, brand, etc.
Recommended architecture
Frontend
- Search input with debounce
- Call
/autocomplete?q=... - Show suggestions
- On select, call
/place/:placeId - Then call
/stores/nearby?lat=...&lng=...&radius=...
Backend
Expose:
GET /autocomplete?q=...GET /place/:placeIdGET /stores/nearby?lat=...&lng=...&radius=...
Your backend can:
- proxy requests to maps provider
- normalize responses
- cache frequent autocomplete queries
- enforce API keys and rate limits
Example data flow
- User types
"cof" - Autocomplete returns:
- Coffee Bean, 123 Main St
- Coffee House, 9 Market St
- User selects Coffee House
- Place lookup returns:
lat: 37.7749lng: -122.4194
- Nearby store search returns:
- Store A, 0.4 km
- Store B, 1.2 km
- Store C, 2.1 km
Implementation tips
Use debounce
Wait ~200–300 ms after typing before calling autocomplete.
Use place IDs
Avoid relying on raw text strings.
Sort by distance
When searching nearby stores, calculate distance with:
- Haversine formula
- geospatial DB index
- provider’s built-in ranking
Add radius and pagination
If there are many stores:
- search within radius
- return the closest first
- paginate or “load more”
Handle empty and error states
- no autocomplete matches
- place lookup fails
- no nearby stores found
- API timeout or quota errors
Example API design
GET /autocomplete?q=piz
GET /place/ChIJ...
GET /stores/nearby?lat=40.7411&lng=-73.9897&radius=5000
Example response:
{
"stores": [
{
"id": "store_1",
"name": "Pizza Palace",
"distance_m": 320,
"address": "12 Broadway, New York, NY"
}
]
}
If you want to build it fast
Use a maps provider that offers:
- autocomplete
- place details
- nearby search
If you want full control over your own store inventory, use:
- your own store database with geospatial indexing
- a separate autocomplete service for locations
Best practice summary
- Autocomplete for user input
- Place lookup to get exact coordinates
- Nearby search against geospatial store data
- Debounce, cache, and rank by distance
- Use place IDs, not raw strings
If you want, I can also give you:
- a React + Node.js example,
- a PostGIS/MongoDB implementation, or
- a Google Maps / Mapbox version.
Brands