2

Using JavaScript in the ArcGIS API, I am querying a feature layer from a Map View, calculating points within a certain radius when the user "click" "drag" with the mouse. I want to be able to stop this functionality when the user presses the spacebar. However, I cannot get this to work. In the JavaScript I turn off event propagation for the "click" "drag" event and then cannot turn it back on again. I understand there is not a "on/off" switch for stopPropagation(). Can anyone recommend a different solution or workaround. An extract of the code I am struggling with below:

var bufferEnabled = false;

function keyDownListener(event) {
  var keyInput = event.key;
  bufferEnabled =
    (keyInput === " " || keyInput === "Spacebar") && !bufferEnabled;
  }

function analysis() {
view.when().then(function() {

  const layer = webmap.layers.getItemAt(0);

  view.whenLayerView(layer).then(function(layerView) {
    watchUtils.whenFalseOnce(layerView, "updating", function(val) {
      // Query layer view statistics as the user clicks
      // or drags the pointer across the view.
      view.on(["click", "drag"], function(event) {
        // disables navigation by pointer drag
        event.stopPropagation();
        queryStatsOnDrag(layerView, event).then(updateCharts);
      });
    });
  });
});

    view.on("key-down", keyDownListener);
    view.on("key-down", analysis);
  • Was there any response to this one? Thank you. – CGarden Jun 3 at 10:59
0

view.on(...) returns an event object. So save a reference to that by setting a variable (var myEvent = view.on(.....) and then when the user indicates to toggle off, just remove the event (myEvent.remove())

  • I don't know how to do that. You're going to have to spell it out if you will. And thank you for your reply. – CGarden Jun 4 at 17:31
  • If someone could elaborate on the above that would be really helpful. – CGarden Jun 6 at 13:00
  • Even though it's for 3.x, this page has information on removing events. You could also use on's pausable method – kenbuja Jul 3 at 18:09
  • @kenbuja I'll take a look, thank you for that. – CGarden Jul 19 at 13:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.