Styling Markers on Mapbox Static Maps
Oct 5, 2019Recently I was using the python map-box api to generate static maps. I needed to style the markers which pin pointed locations on the map. I found it difficult to find the necessary documentation on how to do this. In the end it simply involved adding the following fields:
- maker-colour (hex value).
- marker-symbol (Maki icon name. an integer, or a lowercase letter).
- marker-size (small, large).
To the JSON like so:
origin = {
'type': 'Feature',
'properties': {
'name': 'Cambridge',
'marker-color': '#f600f6',
'marker-symbol': 'c',
'marker-size': 'large',
},
'geometry': {
'type': 'Point',
'coordinates': [0.1218,52.2053]
},
}
Here is the output:
Here is the full code sample:
from mapbox import StaticStyle
location1 = {
'type': 'Feature',
'properties': {
'name': 'Cambridge',
'marker-color': '#f600f6',
'marker-symbol': 'c',
'marker-size': 'large',
},
'geometry': {
'type': 'Point',
'coordinates': [0.1218,52.2053]
},
}
location2 = {
'type': 'Feature',
'properties': {
'name': 'Kings Cross London',
'marker-color': '#26fae4',
'marker-symbol': 'k',
'marker-size': 'large',
},
'geometry': {
'type': 'Point',
'coordinates': [-0.125250,51.544180]
}
}
features = [location1, location2]
service = StaticStyle()
response = service.image(username='mapbox',
style_id='streets-v9',
retina=True,
attribution=False,
logo=False,
features=features)
with open('map.png', 'wb') as img:
img.write(response.content)
Note: Remember to the set the environment variable:
MAPBOX_ACCESS_TOKEN="MY_ACCESS_TOKEN"
Before you run your script, the environment variable is read by the mapbox library.
Mapbox static maps documentation.
Fin.