Changing Mapstyles with Select Box?

I was wondering if anyone has had any luck with being able to change map view styles with a selectbox?

mapstyle = st.sidebar.selectbox(‘Choose Map Style:’, options=[‘Light’, ‘Dark’, ‘Satellite’, ‘Road’])
if (mapstyle == ‘Light’):
pp = pdk.Deck(initial_view_state=view_state, map_provider=‘mapbox’, map_style=pdk.map_styles.CARTO_LIGHT, layers=[slayer, layert1, olayer], tooltip=tooltip)
elif (mapstyle == ‘Dark’):
pp = pdk.Deck(initial_view_state=view_state, map_provider=‘mapbox’, map_style=pdk.map_styles.CARTO_DARK, layers=[slayer, layert1, olayer], tooltip=tooltip)
elif (mapstyle == ‘Satellite’):
pp = pdk.Deck(initial_view_state=view_state, map_provider=‘mapbox’, map_style=pdk.map_styles.SATELLITE, layers=[slayer, layert1, olayer], tooltip=tooltip)
else: (mapstyle == ‘Road’)
pp = pdk.Deck(initial_view_state=view_state, map_provider=‘mapbox’, map_style=pdk.map_styles.CARTO_ROAD, layers=[slayer, layert1, olayer], tooltip=tooltip)

Just curious if anyone has had any luck doing this?

Thanks in advance!
-Justin

Hi @andthisjustin :wave:

From the pydeck documentation, it looks like the type of the map_style argument is (str or dict , default ‘dark’ ) – One of ‘light’, ‘dark’, ‘road’, ‘satellite’, ‘dark_no_labels’, and ‘light_no_labels’.

Solution

Here’s a reproducible example of changing map styles with a selectbox:

import streamlit as st
import pydeck as pdk
import pandas as pd
import numpy as np

df = pd.DataFrame(
    np.random.randn(1000, 2) / [50, 50] + [37.76, -122.4], columns=["lat", "lon"]
)

mapstyle = st.sidebar.selectbox(
    "Choose Map Style:",
    options=["light", "dark", "satellite", "road"],
    format_func=str.capitalize,
)

st.pydeck_chart(
    pdk.Deck(
        map_style=f"{mapstyle}",  # 'light', 'dark', 'satellite', 'road'
        initial_view_state=pdk.ViewState(
            latitude=37.76,
            longitude=-122.4,
            zoom=11,
            pitch=50,
        ),
        layers=[
            pdk.Layer(
                "ScatterplotLayer",
                data=df,
                get_position="[lon, lat]",
                get_color="[200, 30, 0, 160]",
                get_radius=200,
            ),
        ],
    )
)

Output

pydeck-map-style

Hope this helps!

Best, :balloon:
Snehan

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.