11from __future__ import annotations
22
33import warnings
4+ from dataclasses import replace
45from pathlib import Path
56from typing import IO , TYPE_CHECKING , Any , TextIO , overload
67
2021 ScaleControl ,
2122)
2223from lonboard .layer import BaseLayer
24+ from lonboard .models import BaseViewState , MapViewState
2325from lonboard .traits import HeightTrait , VariableLengthTuple , ViewStateTrait
24- from lonboard .traits ._map import DEFAULT_INITIAL_VIEW_STATE
2526from lonboard .view import BaseView
2627
2728if TYPE_CHECKING :
@@ -154,9 +155,7 @@ def on_click(self, callback: Callable, *, remove: bool = False) -> None:
154155 _esm = bundler_output_dir / "index.js"
155156 _css = bundler_output_dir / "index.css"
156157
157- # TODO: change this view state to allow non-map view states if we have non-map views
158- # Also allow a list/tuple of view states for multiple views
159- view_state = ViewStateTrait ()
158+ view_state : BaseViewState | None = ViewStateTrait () # type: ignore
160159 """
161160 The view state of the map.
162161
@@ -492,8 +491,9 @@ def add_layer(
492491 elif reset_zoom :
493492 self .view_state = compute_view (self .layers ) # type: ignore
494493
495- def set_view_state (
494+ def set_view_state ( # noqa: PLR0913
496495 self ,
496+ view_state : BaseViewState | None = None ,
497497 * ,
498498 longitude : float | None = None ,
499499 latitude : float | None = None ,
@@ -505,6 +505,9 @@ def set_view_state(
505505
506506 Any parameters that are unset will not be changed.
507507
508+ Args:
509+ view_state: A complete view state object to set on the map.
510+
508511 Keyword Args:
509512 longitude: the new longitude to set on the map. Defaults to None.
510513 latitude: the new latitude to set on the map. Defaults to None.
@@ -513,24 +516,29 @@ def set_view_state(
513516 bearing: the new bearing to set on the map. Defaults to None.
514517
515518 """
516- view_state = (
517- self .view_state ._asdict () # type: ignore
518- if self .view_state is not None
519- else DEFAULT_INITIAL_VIEW_STATE
520- )
521-
522- if longitude is not None :
523- view_state ["longitude" ] = longitude
524- if latitude is not None :
525- view_state ["latitude" ] = latitude
526- if zoom is not None :
527- view_state ["zoom" ] = zoom
528- if pitch is not None :
529- view_state ["pitch" ] = pitch
530- if bearing is not None :
531- view_state ["bearing" ] = bearing
532-
533- self .view_state = view_state
519+ if view_state is not None :
520+ self .view_state = view_state
521+ return
522+
523+ current_view_state = self .view_state
524+ if isinstance (current_view_state , MapViewState ):
525+ changes = {}
526+ if longitude is not None :
527+ changes ["longitude" ] = longitude
528+ if latitude is not None :
529+ changes ["latitude" ] = latitude
530+ if zoom is not None :
531+ changes ["zoom" ] = zoom
532+ if pitch is not None :
533+ changes ["pitch" ] = pitch
534+ if bearing is not None :
535+ changes ["bearing" ] = bearing
536+
537+ self .view_state = replace (current_view_state , ** changes )
538+ else :
539+ raise TypeError (
540+ "Can only set MapViewState or GlobeViewState parameters individually via set_view_state.\n For other view state types, pass a complete view_state object." ,
541+ )
534542
535543 def fly_to ( # noqa: PLR0913
536544 self ,
0 commit comments