Skip to content

bounds parameter fails on Quarto dashboard page navigation #131

@RayLarone

Description

@RayLarone

The issue occurs because mapgl widgets in non-active Quarto dashboard pages initialize with zero container dimensions, causing the bounds parameter to be ignored - the map renders but remains zoomed out considerably rather than fitting to the specified bounds. A workaround using ResizeObserver in htmlwidgets::onRender() detects when the container gets actual dimensions (when the dashboard page becomes visible) and then calls map.resize() followed by map.fitBounds() to properly fit the bounds. Interestingly, leaflet widgets handle this scenario correctly without any workaround, suggesting that leaflet has built-in resize handling that respects bounds parameters even when containers start with zero dimensions. The ResizeObserver approach successfully mimics this behavior, but ideally this resize detection should be built into the core mapgl widget to provide seamless Quarto dashboard compatibility out of the box.

---
title: "mapgl bounds bug"
format: dashboard
---

```{r}
library(sf)
library(dplyr)
library(mapgl)

london <- read_sf("https://gist.githubusercontent.com/cejast/2cb80a2346b2049ac5d0/raw/c7ea98c7b9204782b6652b29bf3ef3e3b1a187ea/london-topojson.json", crs=4326)
```

# Page 1

##

```{r}
maplibre(bounds=london) |>
  add_line_layer(source = london, id="test")
```

# Page 2

##

```{r}
maplibre(bounds=london) |>
  add_line_layer(source = london, id="test2") 
```

Page 1

Image

Page 2

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions