πŸ—ΊοΈ 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 πŸš€.