πΊοΈ Put a Map on Your Website
J.Gong
2025-08-27
1.16min
This was totally unexpected. Weeks ago, I asked Cursor to scan all my files and add a city parameter, like βTallinn, Estoniaβ, to show where I wrote each post π.
Yesterday, I thought: since so many documents are already marked, why not add a map to the website? π Also, I was curious about how many places Iβve been.
π οΈ Adding the Map
Adding a map to a page is not hard:
- Google Maps / Mapbox β mature solutions, barely any code needed β
- Downside β requires paid tokens πΈ
Since I didnβt want to spend too much, I looked for open-source solutions.
The answer: MapLibre GL JS π.
For React, thereβs a component: react-map-gl βοΈ.
For a free base map, Cartoβs Positron Style π Carto Positron.
Best part: no token required π.
π The Hard Part β Getting City Coordinates
Showing cities on the map means I need their latitude & longitude. This was trickier than I expected:
- Tried cities.json β includes many major cities, but not suitable for my case β
- Tried china-division β good data, but only in Chinese π
- Tried pinyin to translate β unstable results β οΈ
Finally, I fell back to Google Maps Geocoding API:
https://maps.googleapis.com/maps/api/geocode/json?address=${data.city[i]}&key=${GOOGLE_API_KEY}
It works well, even with vague searches. To avoid exceeding the API quota, I wrote a script to cache/save the coordinates π.
And now π here I can see all my articles pinned on a map πβ¨.
β Conclusion
What started as a small βadd city infoβ experiment became a map visualization project.
- Open-source tools like MapLibre + react-map-gl + Carto basemap give you free, flexible mapping options π
- For coordinates, Google Maps API is still the most reliable choice π
- With a bit of scripting, you can cache results and avoid extra costs π§βπ»
Now my blog not only shows what I write, but also where I write it βοΈπβ turning content into a travel map of my life π.