If you're looking to embed interactive maps in your Python applications, Folium and Geoapify make it incredibly easy.
Whether you're visualizing geographic data, planning logistics, or just want to explore cities from your script, this tutorial shows you how to generate a map with custom styling using Geoapify tiles and display it in a web browser using the Folium library.
Let’s dive in and build your own interactive map in less than 10 minutes!
🐙💻 Code Repository
The full working example is available on GitHub:
👉 Geoapify Maps API Python Example
Clone it or download the script to get started right away!
What You'll Build
You’ll create a Python script that:
- Generates an interactive Leaflet map using Geoapify raster tiles.
- Supports command-line options for zoom level, style, coordinates, and API key. Sign up and get you Free API Key on Geoapify.
- Adds a marker (Eiffel Tower in our example).
- Saves the map as an HTML file and opens it in the browser.
How to Run
Install dependencies:
pip install folium requests
Run the script with your Geoapify API key:
python interactive_map.py --style osm-bright --zoom 12 --lat 48.8566 --lon 2.3522 --api-key=your-api-key
Key Code Highlights
Here’s a quick overview of what happens under the hood:
1. Dynamic Tile URL with Geoapify
BASE_URL = "https://maps.geoapify.com/v1/tile/{map_style}/{{z}}/{{x}}/{{y}}@2x.png?apiKey={api_key}"
Geoapify’s raster tiles are loaded based on the style and API key you provide.
2. Creating the Folium Map
m = folium.Map(location=[lat, lon], zoom_start=zoom)
The map is centered at the coordinates passed via arguments.
3. Adding Geoapify Tiles
folium.TileLayer(tiles=tile_url, name='Geoapify Map', attr=...)
Custom map tiles from Geoapify are added with attribution.
4. Adding a Marker
folium.Marker(location=[48.8584, 2.2945], popup='Eiffel Tower').add_to(m)
By default, the map displays a red pin for the Eiffel Tower.
5. Saving the Map
map_object.save('map.html')
The resulting HTML is saved locally and opened in the default browser.
🧠 Why Use Geoapify?
- Free tier available with generous limits
- Easy-to-integrate map APIs
- Modern, beautiful map styles
- Full documentation
Conclusion
With just a few lines of Python code, you can generate stunning, interactive maps for data visualization or mapping applications. Geoapify + Folium = a simple, powerful solution!
Explore the full example on GitHub:
📦 https://github.com/geoapify/maps-api-code-samples/tree/main/python/create-a-map
Top comments (0)