๐บ๏ธ Put a Map on Your Website
J.Gong
2025-08-27
1.16min
๐บ๏ธ Put a Map on Your Website
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 ๐.