๐Ÿ—บ๏ธ 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 ๐Ÿš€.