How to reset bearing on pydeck map to 'true north'?

Summary

I am working on an app that displays 2 Pydeck maps, one that’s a regular 2D and one that shows a layer in 3D. I would like to have a button or some way to reset the rotation of the map to ‘true north’, similar to how Mapbox has a “rotation control” button below the zoom buttons (see an example here).

I know there’s a bearing parameter you can set to 0 when using pdk.Deck, and true enough, when the page first loads, it loads with true north pointing “up.”

But if a user wants to pan around the map in 3D, I would want them to have some way of having the map snap back to true north once more to reorient themselves (without having to refresh the entire browser page).

In the code snippet below, I would expect this would happen when the user toggles between the ‘2D’ and ‘3D’ options in the sidebar, since both fire the mapper() function. Is there a another way to automatically reorient the map after panning and rotating?

Steps to reproduce

Code snippet:

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

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

view = st.sidebar.radio(
    'Select map view:',
    ('3D','2D')
)

view_dict = {
    '2D':False,
    '3D':True
}

def mapper():
    r = pdk.Deck(
        map_style=None,
        initial_view_state=pdk.ViewState(
            latitude=37.76,
            longitude=-122.4,
            zoom=11,
            pitch=50,
            bearing=0
        ),

        layers=[
            pdk.Layer(
            'HexagonLayer',
            data=chart_data,
            get_position='[lon, lat]',
            radius=200,
            elevation_scale=4,
            elevation_range=[0, 1000],
            pickable=True,
            extruded=view_dict[view],
            auto_highlight=True
            ),

            pdk.Layer(
                'ScatterplotLayer',
                data=chart_data,
                get_position='[lon, lat]',
                get_color='[200, 30, 0, 160]',
                get_radius=200,
            ),
        ],
    )

    return r

if view == '3D':
    st.pydeck_chart(mapper())
else:
    st.pydeck_chart(mapper())

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