|
13 | 13 | from lonboard._layer import BaseLayer |
14 | 14 | from lonboard._viewport import compute_view |
15 | 15 | from lonboard.basemap import CartoBasemap |
| 16 | +from lonboard.traits import DEFAULT_INITIAL_VIEW_STATE, ViewStateTrait |
16 | 17 | from lonboard.types.map import MapKwargs |
17 | 18 |
|
18 | 19 | if TYPE_CHECKING: |
@@ -95,32 +96,28 @@ def __init__( |
95 | 96 | _esm = bundler_output_dir / "index.js" |
96 | 97 | _css = bundler_output_dir / "index.css" |
97 | 98 |
|
98 | | - _initial_view_state = traitlets.Dict().tag(sync=True) |
| 99 | + view_state = ViewStateTrait() |
99 | 100 | """ |
100 | | - The initial view state of the map. |
| 101 | + The view state of the map. |
101 | 102 |
|
102 | | - - Type: `dict`, optional |
| 103 | + - Type: [`ViewState`][lonboard.models.ViewState] |
103 | 104 | - Default: Automatically inferred from the data passed to the map. |
104 | 105 |
|
105 | | - The keys _must_ include: |
| 106 | + You can initialize the map to a specific view state using this property: |
106 | 107 |
|
107 | | - - `longitude`: longitude at the map center. |
108 | | - - `latitude`: latitude at the map center. |
109 | | - - `zoom`: zoom level. |
110 | | -
|
111 | | - Keys may additionally include: |
| 108 | + ```py |
| 109 | + Map( |
| 110 | + layers, |
| 111 | + view_state={"longitude": -74.0060, "latitude": 40.7128, "zoom": 7} |
| 112 | + ) |
| 113 | + ``` |
112 | 114 |
|
113 | | - - `pitch` (float, optional) - pitch angle in degrees. Default `0` (top-down). |
114 | | - - `bearing` (float, optional) - bearing angle in degrees. Default `0` (north). |
115 | | - - `maxZoom` (float, optional) - max zoom level. Default `20`. |
116 | | - - `minZoom` (float, optional) - min zoom level. Default `0`. |
117 | | - - `maxPitch` (float, optional) - max pitch angle. Default `60`. |
118 | | - - `minPitch` (float, optional) - min pitch angle. Default `0`. |
| 115 | + !!! note |
119 | 116 |
|
120 | | - Note that currently no camel-case/snake-case translation occurs for this method, and |
121 | | - so keys must be in camel case. |
| 117 | + The properties of the view state are immutable. Use |
| 118 | + [`set_view_state`][lonboard.Map.set_view_state] to modify a map's view state |
| 119 | + once it's been initially rendered. |
122 | 120 |
|
123 | | - This API is not yet stabilized and may change in the future. |
124 | 121 | """ |
125 | 122 |
|
126 | 123 | _height = traitlets.Int(default_value=DEFAULT_HEIGHT, allow_none=True).tag( |
@@ -268,12 +265,51 @@ def __init__( |
268 | 265 | global `parameters` when that layer is rendered. |
269 | 266 | """ |
270 | 267 |
|
| 268 | + def set_view_state( |
| 269 | + self, |
| 270 | + *, |
| 271 | + longitude: Optional[float] = None, |
| 272 | + latitude: Optional[float] = None, |
| 273 | + zoom: Optional[float] = None, |
| 274 | + pitch: Optional[float] = None, |
| 275 | + bearing: Optional[float] = None, |
| 276 | + ) -> None: |
| 277 | + """Set the view state of the map. |
| 278 | +
|
| 279 | + Any parameters that are unset will not be changed. |
| 280 | +
|
| 281 | + Other Args: |
| 282 | + longitude: the new longitude to set on the map. Defaults to None. |
| 283 | + latitude: the new latitude to set on the map. Defaults to None. |
| 284 | + zoom: the new zoom to set on the map. Defaults to None. |
| 285 | + pitch: the new pitch to set on the map. Defaults to None. |
| 286 | + bearing: the new bearing to set on the map. Defaults to None. |
| 287 | + """ |
| 288 | + view_state = ( |
| 289 | + self.view_state._asdict() # type: ignore |
| 290 | + if self.view_state is not None |
| 291 | + else DEFAULT_INITIAL_VIEW_STATE |
| 292 | + ) |
| 293 | + |
| 294 | + if longitude is not None: |
| 295 | + view_state["longitude"] = longitude |
| 296 | + if latitude is not None: |
| 297 | + view_state["latitude"] = latitude |
| 298 | + if zoom is not None: |
| 299 | + view_state["zoom"] = zoom |
| 300 | + if pitch is not None: |
| 301 | + view_state["pitch"] = pitch |
| 302 | + if bearing is not None: |
| 303 | + view_state["bearing"] = bearing |
| 304 | + |
| 305 | + self.view_state = view_state |
| 306 | + |
271 | 307 | def fly_to( |
272 | 308 | self, |
273 | 309 | *, |
274 | 310 | longitude: Union[int, float], |
275 | 311 | latitude: Union[int, float], |
276 | | - zoom: int, |
| 312 | + zoom: float, |
277 | 313 | duration: int = 4000, |
278 | 314 | pitch: Union[int, float] = 0, |
279 | 315 | bearing: Union[int, float] = 0, |
@@ -333,6 +369,6 @@ def to_html( |
333 | 369 | drop_defaults=False, |
334 | 370 | ) |
335 | 371 |
|
336 | | - @traitlets.default("_initial_view_state") |
| 372 | + @traitlets.default("view_state") |
337 | 373 | def _default_initial_view_state(self): |
338 | 374 | return compute_view(self.layers) |
0 commit comments