MapView Extent-Change

5176
5
Jump to solution
05-09-2016 02:36 PM
PaulYoum1
New Contributor II

In the Javascript API 4.0, I believe I'm to interact with the map via a MapView if I'm doing a 2D map. I couldn't help but notice that the events for the MapView are pretty bare, with only 4 supported events. If I wanted to have the map event of "extent-change" like in 3.16, is there a different process to hook into this event in 4.0? Or is this a feature to be added in the future?

0 Kudos
1 Solution

Accepted Solutions
ReneRubalcava
Frequent Contributor

It's actually much easier to do in 4.0 now, because you don't need to know event strings to listen to. You want to know when view.extent changes, so you can just do:

view.watch("extent", function(newValue, oldValue) {});

And that's it.

You can see more in the docs here. Working with properties | ArcGIS API for JavaScript 4.0

And I've done a lot of posts on the Accessor, which is what powers all this, here.

View solution in original post

5 Replies
ReneRubalcava
Frequent Contributor

It's actually much easier to do in 4.0 now, because you don't need to know event strings to listen to. You want to know when view.extent changes, so you can just do:

view.watch("extent", function(newValue, oldValue) {});

And that's it.

You can see more in the docs here. Working with properties | ArcGIS API for JavaScript 4.0

And I've done a lot of posts on the Accessor, which is what powers all this, here.

SteveCole
Frequent Contributor

What about if you want to cancel the given event you're "listening" to? For example, prevent zooming in/out beyond a certain level or extent, etc..

0 Kudos
ReneRubalcava
Frequent Contributor

The watch method returns what  is called a WatchHandle that has a remove() method so you can stop listening for property changes.

Accessor | API Reference | ArcGIS API for JavaScript 4.0

The prevent zooming in/out beyond extent stuff is separate. You would want to look at the constraints of the View to define those. MapView | API Reference | ArcGIS API for JavaScript 4.0

SteveCole
Frequent Contributor

The extent example was the quickest thing I could think of that would illustrate that idea. Something else would be like aborting an apply edits command (which, I know, isn't yet in Version 4). Unlike what I was asking about, it sounds like accessors are geared towards "events" that you want to have happen then, no?

0 Kudos
ReneRubalcava
Frequent Contributor

That would be an asynchronous task.

Watching for property changes doesn't necessarily replace events. There are still events in the API, but watching for property changes let's you get real specific on what it is you care about that might change on an object.

It sounds like you want to be able to cancel something like a QueryTask, or the applyEdits or similar. In that case, we don't really have a mechanism in place to cancel a task. It's something we can look at, but once the request is in flight, we could only at best ignore the result, it doesn't mean the request didn't go through to the service and your edits didn't apply though. Once the request is at the server, not much we can do to cancel that.