Menu

1 / 2

Building a City Autocomplete with 150K Cities — Tutorial

DEV Community·ApogeoAPI·7 months ago
#gGPLTzAH
#react#cities#city#results#search
Reading 0:00
15s threshold

City autocomplete sounds simple until you realize you need to search across 150,000 cities from 250 countries — with fast response times and no UI jank. Here's how to do it right.

The Challenge

You can't load 150K cities into the browser — that's tens of megabytes. You need server-side search that returns relevant results as the user types. ApogeoAPI's global search endpoint handles this.

The API Approach

Use the global search endpoint for cross-country city search:

GET /v1/search?q=lon&limit=5 // Returns cities, states, and countries matching "lon" // Results: London GB, Long Beach US, Longueuil CA, ...

Enter fullscreen mode

Exit fullscreen mode

Step 1: Build the Search Hook with Debounce

// hooks/useCitySearch.ts import { useState , useEffect } from ' react ' ; function useDebounce ( value : T , ms : number ): T { const [ debounced , setDebounced ] = useState ( value ); useEffect (() => { const timer = setTimeout (() => setDebounced ( value ), ms ); return () => clearTimeout ( timer ); }, [ value…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.