Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Layer switcher not necessarily updated when layers are removed from the map #174

Open
symbioquine opened this issue Jul 27, 2022 · 1 comment

Comments

@symbioquine
Copy link
Collaborator

symbioquine commented Jul 27, 2022

We probably need to update this to honor layers being removed(/changed?):

// When new layers are added, refresh the layer layer switcher
instance.map.on('farmOS-map.layer', () => {
renderLayerSwitcher();
});

Also relevant: walkermatt/ol-layerswitcher#98

@mstenta
Copy link
Member

mstenta commented Jul 27, 2022

Here's some behavior code to replicate against - this is where we discovered the issue. The behavior handles copying WKT from the input field under the map into the drawing layer.

(function () {
  farmOS.map.behaviors.wkt_refresh = {
    attach: function (instance) {

      // Get the wkt input element.
      var wkt = document.getElementById('edit-mymap-value');

      // Run a handleInput() callback when input changes.
      wkt.oninput = handleInput;
      function handleInput(e) {
        if (wkt.value) {

          // Clear features from the current edit layer's source.
          instance.edit.layer.getSource().clear();

          // Add a new temporary invisible WKT layer.
          var layer = instance.addLayer('wkt', {
            title: 'WKT',
            wkt: wkt.value,
            visible: false,
          });

          // Copy features from the WKT layer to the edit layer.
          instance.edit.layer.getSource().addFeatures(layer.getSource().getFeatures());

          // Remove the temporary WKT layer.
          instance.map.removeLayer(layer);
          
          // Zoom to the edit layer.
          instance.zoomToLayer(instance.edit.layer);
        } else if (wkt.value === '') {

          // Clear features from the current edit layer's source.
          instance.edit.layer.getSource().clear();
        }
      }
    },

    // Make sure this runs after farmOS.map.behaviors.wkt.
    weight: 101
  };
}());

(Note this currently assumes a map named mymap for the ID matching in var wkt = document.getElementById('edit-mymap-value');)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants