4

I am trying to add an event listener for when the scale of the map changes, that is scale or zoom level changes in order to enable or disable feature layer label text. When the user zooms in to a larger scale I want the labels to be visible and when the scale is lowered the labels should go away. After defining the MapView I tried the following

 view.on("zoom", function(){alert();});

2 Answers 2

5

The practice in version 3.x of the ArcGIS API for JavaScript of using event listeners has been superseded in 4.x by a new paradigm: watchable properties.

from the documentation:

Prior to 4.0, property changes were handled with events. In 4.0, watching for property changes has become much easier. This is handled via the .watch(property, callback) method. The callback is called each time the property changes and allows you to work with the property's new value, old value, and name along with the watched object.

this is what a generic callback looks like:

var propertyChangeHandler = function(newValue, oldValue, property, object){  
  console.log("New Value: ",newValue," Old Value: ",oldValue,
  " Changed Property: ",property," Watched Object: ",object);
};  

the benefit is that you are no longer restricted to listening for a subset of events bubbled up from within the API. instead, all properties can now be checked for changes.

3
  • Certainly, I understand your point, thanks for the clarification.
    – whyzar
    Jan 6, 2017 at 0:57
  • Thanks for the clarification I will look into this new 'watchable' change
    – kabuto178
    Jan 6, 2017 at 16:17
  • @whyzar and OP my pleasure! Jan 6, 2017 at 22:30
1

You could try to set up the event listener as follows. More information here

// Listen to the click event on the map view.
view.on("click", function(event) {
console.log("click event: ", event.mapPoint);
});

The sample also shows how to listen to events. It listens to the change event on the map's allLayers property. In this case, we only track when layers are added to the map. It also listens to the layerview-create event on the view to get notified when each layer in the map has a corresponding LayerView and is rendered in the view. Lastly, we listen to the click event on the view

5
  • the OP could have been more explicit, but its evident by his reference to MapView that he is not working with version 3.x of the JSAPI: developers.arcgis.com/javascript/latest/api-reference/… Jan 6, 2017 at 0:44
  • Yes, the sample code and reference page I provided is from version 4.x. I'm not sure what you are viewing.
    – whyzar
    Jan 6, 2017 at 0:45
  • because its 4.x, an event listener is irrelevant here. because he's interested in scale changes, a click listener is even more irrelevant. Jan 6, 2017 at 0:47
  • i intended to include a "zoom" versus the "click", that was just the sample code I referenced to.
    – whyzar
    Jan 6, 2017 at 0:49
  • as i pointed out in my own answer, event listeners for checking things like zoom and scale have been deprecated in 4.x in favor of watchable properties. Jan 6, 2017 at 0:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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