|
| 1 | +# Python Interactive Map Example |
| 2 | + |
| 3 | +This project demonstrates how to use the [Geoapify Maps Tiles](https://www.geoapify.com/map-tiles/) to display an interactive raster map using [folium](https://pypi.org/project/folium/). The map includes customizable styles, zoom levels, and center coordinates. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## **Features** |
| 8 | +- Uses Geoapify Maps API for high-quality map tiles. |
| 9 | +- Supports various map styles. |
| 10 | +- Allows customization of zoom level and map center. |
| 11 | +- Displays an interactive map with an Eiffel Tower marker. |
| 12 | +- Automatically validates API keys and handles errors. |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +## **Requirements** |
| 17 | + |
| 18 | +Ensure you have the following installed: |
| 19 | + |
| 20 | +1. Python 3.11 or higher |
| 21 | +2. pip (Python package manager) |
| 22 | + |
| 23 | +## **Setup Instructions** |
| 24 | + |
| 25 | +### 1. Clone the Repository |
| 26 | + |
| 27 | +```bash |
| 28 | +git clone https://geoapify.github.io/maps-api-code-samples/ |
| 29 | +cd maps-api-code-samples/python |
| 30 | +``` |
| 31 | + |
| 32 | +### 2. Create a Virtual Environment (Optional) |
| 33 | + |
| 34 | +It’s recommended to use a virtual environment to avoid dependency conflicts: |
| 35 | + |
| 36 | +```bash |
| 37 | +python -m venv env |
| 38 | +source env/bin/activate # On Windows: env\Scripts\activate |
| 39 | +``` |
| 40 | + |
| 41 | +### 3. Install Dependencies |
| 42 | + |
| 43 | +Install the required Python libraries using pip: |
| 44 | + |
| 45 | +```bash |
| 46 | +pip install folium requests |
| 47 | +``` |
| 48 | + |
| 49 | +--- |
| 50 | + |
| 51 | +## **Running the Example** |
| 52 | + |
| 53 | +Run the script to generate an interactive map: |
| 54 | + |
| 55 | +```bash |
| 56 | +cd create-a-map |
| 57 | +python interactive_map.py --style osm-bright --zoom 12 --lat 48.8566 --lon 2.3522 --api-key=YOUR_API_KEY |
| 58 | +``` |
| 59 | + |
| 60 | +### **Command-line Arguments** |
| 61 | +- `--api-key` (required): API key for Geoapify services. |
| 62 | +- `--style` (optional, default: `osm-carto`): Map style. |
| 63 | +- `--zoom` (optional, default: `17`): Zoom level. |
| 64 | +- `--lat` (optional, default: `48.8584`): Latitude for the map center. |
| 65 | +- `--lon` (optional, default: `2.2945`): Longitude for the map center. |
| 66 | + |
| 67 | +--- |
| 68 | + |
| 69 | +## Code Explanation |
| 70 | + |
| 71 | +The `create_map` function initializes an interactive map using `folium`. It takes the map style, zoom level, latitude, longitude, and API key as parameters to generate a customized map with Geoapify tiles. |
| 72 | + |
| 73 | +```python |
| 74 | +import folium |
| 75 | + |
| 76 | +BASE_URL = "https://maps.geoapify.com/v1/tile/{map_style}/{{z}}/{{x}}/{{y}}@2x.png?apiKey={api_key}" |
| 77 | + |
| 78 | +def create_map(map_style, zoom, lat, lon, api_key): |
| 79 | + """ |
| 80 | + Creates an interactive map using Geoapify tiles. |
| 81 | +
|
| 82 | + Parameters: |
| 83 | + - map_style (str): The style of the map (e.g., 'osm-carto', 'osm-bright'). |
| 84 | + - zoom (int): The initial zoom level. |
| 85 | + - lat (float): Latitude for the map center. |
| 86 | + - lon (float): Longitude for the map center. |
| 87 | + - api_key (str): API key for accessing Geoapify services. |
| 88 | +
|
| 89 | + Returns: |
| 90 | + - folium.Map object containing the configured map. |
| 91 | + """ |
| 92 | + |
| 93 | + # Initialize the folium map centered at the specified location |
| 94 | + m = folium.Map(location=[lat, lon], zoom_start=zoom) |
| 95 | + |
| 96 | + # Construct the tile layer URL using the provided API key and map style |
| 97 | + tile_url = BASE_URL.format(map_style=map_style, api_key=api_key) |
| 98 | + |
| 99 | + # Add Geoapify tile layer to the map |
| 100 | + folium.TileLayer( |
| 101 | + tiles=tile_url, |
| 102 | + name="Geoapify Map", |
| 103 | + attr="""Powered by <a href="https://www.geoapify.com/" target="_blank">Geoapify</a> |
| 104 | + | <a href="https://openmaptiles.org/" rel="nofollow" target="_blank">© OpenMapTiles</a> |
| 105 | + | <a href="https://www.openstreetmap.org/copyright" rel="nofollow" target="_blank">© OpenStreetMap</a> contributors""", |
| 106 | + overlay=True, |
| 107 | + control=True |
| 108 | + ).add_to(m) |
| 109 | + |
| 110 | + # Add a marker at the Eiffel Tower location for demonstration purposes |
| 111 | + folium.Marker( |
| 112 | + location=[48.8584, 2.2945], |
| 113 | + popup="Eiffel Tower", |
| 114 | + icon=folium.Icon(color="red"), |
| 115 | + ).add_to(m) |
| 116 | + |
| 117 | + return m |
| 118 | +``` |
| 119 | + |
| 120 | +### **Explanation** |
| 121 | +1. **Initialize the Map** |
| 122 | + - Uses `folium.Map()` to create a base map centered at the provided latitude and longitude. |
| 123 | + - The `zoom_start` parameter determines the initial zoom level. |
| 124 | + |
| 125 | +2. **Add Tile Layer** |
| 126 | + - The tile layer URL is formatted using the provided `map_style` and `api_key` values. |
| 127 | + - The tile layer is added using `folium.TileLayer()`, ensuring that Geoapify map styles are applied. |
| 128 | + |
| 129 | +3. **Add a Marker** |
| 130 | + - A red marker is placed at the Eiffel Tower's coordinates (`48.8584, 2.2945`). |
| 131 | + - The marker has a popup message that appears when clicked. |
| 132 | + |
| 133 | +4. **Return the Map** |
| 134 | + - The function returns a `folium.Map` object that can be saved as an HTML file or displayed in a browser. |
| 135 | + |
| 136 | +This function allows users to create a customizable interactive map with Geoapify tiles, supporting different styles and zoom levels. |
| 137 | + |
| 138 | +--- |
| 139 | + |
| 140 | +## **Output** |
| 141 | +- The script generates a `map.html` file. |
| 142 | +- The browser automatically opens the generated map. |
| 143 | +- If the browser fails to open, manually open `map.html` in any web browser. |
| 144 | + |
| 145 | +## **Error Handling** |
| 146 | +- If an invalid map style is provided, the script falls back to `osm-carto`. |
| 147 | +- If an invalid API key is provided, the script exits with an error. |
| 148 | +- If a server error occurs (5XX response), the script stops execution. |
| 149 | + |
| 150 | +## **Notes** |
| 151 | +- Ensure that you have a valid [Geoapify API key](https://www.geoapify.com/) before running the script. |
| 152 | +- The map includes attribution links to comply with OpenStreetMap and Geoapify usage policies. |
| 153 | + |
| 154 | +## **License** |
| 155 | +This project is licensed under the MIT License. |
| 156 | + |
0 commit comments