Skip to content

(Request) Expose beforeMap and afterMap in compare #153

@mhpob

Description

@mhpob

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!

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