mapgl icon indicating copy to clipboard operation
mapgl copied to clipboard

(Request) Expose beforeMap and afterMap in compare

Open mhpob opened this issue 1 month ago • 0 comments

I'd like to request that the beforeMap and afterMap variables be exposed in the respective X_compare.js, I imagine in a similar way to the getMap function in mapboxgl.js and maplibregl.js.

The hope would be to allow the maps to be edited via htmlwidgets::onRender like the below (a toy example that adds a marker with different colors to each map).

mapgl::compare(
  mapgl::maplibre(center = c(-71.04, 42.36), zoom = 11) ,
  mapgl::maplibre(center = c(-71.04, 42.36), zoom = 11)
) |> 
  htmlwidgets::onRender(
    "function(el, x){
      m1 = this.getBeforeMap();
      m2 = this.getAfterMap();

      m1.on('load', async () => {
       const marker = new maplibregl.Marker()
        .setLngLat([-71.04, 42.36])
        .addTo(m1);
      })

      m2.on('load', async () => {
       const marker = new maplibregl.Marker({color: '#00FF00'})
        .setLngLat([-71.04, 42.36])
        .addTo(m2);
      })
   }"
)

FWIW, this worked on a local fork after adding the following to the js script:

getBeforeMap: function () {
    return beforeMap;
},

getAfterMap: function () {
    return afterMap;
}

Happy to submit a PR adding those to the two compare.js files!

mhpob avatar Nov 05 '25 21:11 mhpob