Files
GenerateMapboxMarkers/README.md
T
2026-10-03 12:26:10 +02:00

81 lines
2.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
If you need to create a map on Wordpress with around many markers, the following script should help you.
The Python script will:
- Read `name,address` rows from a CSV file.
- Send each address to Mapbox’s forward-geocoding API.
- Extract the returned place name, Mapbox feature ID, longitude, and latitude.
- Generate a complete `wp:jetpack/map` block that you can paste into the WordPress code editor.
Mapbox’s geocoder converts address text into coordinates, and its response includes a feature ID and human-readable place name suitable for constructing the Jetpack point objects.
## Expected CSV
Save a file called `addresses.csv`:
```csv
name,address
Alice,"1 rue de la Paix, Paris, France"
Bob,"3 avenue Foch, Paris, France"
```
The first column becomes the marker title. The second column is sent to Mapbox.
## Running it
Install the required library:
```bash
python -m pip install requests
```
Set your Mapbox token.
On macOS/Linux:
```bash
export MAPBOX_TOKEN="pk.your_mapbox_token_here"
```
On Windows PowerShell:
```powershell
$env:MAPBOX_TOKEN="pk.your_mapbox_token_here"
```
Run the script:
```bash
python generate_jetpack_map.py addresses.csv
```
It will create:
```text
jetpack-map.txt
```
You can also specify the output filename:
```bash
python generate_jetpack_map.py addresses.csv \
--output wordpress-map.txt \
--country fr \
--language fr \
--zoom 12
```
The generated file will contain a block that you can paste into Wordpress. Wordpress will complain that the element needs to be repaired, but it should work.
The script calculates `mapCenter` as the average longitude and latitude of all successfully geocoded addresses.
A few practical details:
- `title` comes from the CSV `name` column.
- `placeTitle` and `caption` come from Mapbox’s returned address.
- Failed addresses are excluded from the map and written to a separate `*-failed.csv` file.
- The token is read from an environment variable rather than being written into the script.
- If your addresses are not in France, change `--country`, for example `--country be` or `--country us`.
- If you do not want Mapbox to return French place names, use `--language en`.
The `permanent=True` setting in the script is intentional because the coordinates and Mapbox IDs are being saved into WordPress. If your Mapbox account does not support permanent geocoding, remove that setting only after confirming that your intended storage and use comply with Mapbox’s current terms.