How to get the proper extent based on the data returned from map service

502
3
Jump to solution
10-29-2020 02:03 PM
MichelTurmel
New Contributor III

I am using Web AppBuilder 2.6 to create a widget.

I have an ArcGIS Map Service that contains the name of several companies and their offices' location in north America. The user interface provides a list of the company names, the user selects a company name and the service returns the offices' location for the requested company name and then the new layer is added to the map. In some instances, the offices are located in one state in other instances the offices are located all across  North America.

My question is how can I find the appropriate extent so that all offices' locations are present under the presented map?

In other words, if all returned offices' location are in one state then the map view shows only that state and in the case where the offices' location  are allover North America then the map view would show all North America.

I understand that I can set the extent programmatically as the map is initialized within the widget. I need a method to find the appropriate extent.

0 Kudos
1 Solution

Accepted Solutions
MichelTurmel
New Contributor III

Here is what I have done:

........

var aSearch = "OP LIKE 'something'";
var queryTask = new QueryTask("https://.../MapServer/0");
var query = new Query();
query.returnGeometry = true;
query.returnCountOnly = false;
query.outFields = ["OP"];
query.where = aSearch;

queryTask.execute(query, this.aShowResults);

....
aShowResults: function(results){
aCount = results.features.length;

if (aCount !== 0) {
aExtent = graphicsUtils.graphicsExtent(results.features);
}

addLayersToMap();
},

......

function addLayersToMap(){
map.addLayers([.......]);
map.setExtent(aExtent);
}

Your help is really appreciated, you sent me in the right direction!

View solution in original post

0 Kudos
3 Replies
RobertScheitlin__GISP
MVP Emeritus

Michel,

  Use the graphicsUtils graphicsextent method

esri/graphicsUtils | API Reference | ArcGIS API for JavaScript 3.34 

0 Kudos
MichelTurmel
New Contributor III

Thank you very much for the information Robert!

0 Kudos
MichelTurmel
New Contributor III

Here is what I have done:

........

var aSearch = "OP LIKE 'something'";
var queryTask = new QueryTask("https://.../MapServer/0");
var query = new Query();
query.returnGeometry = true;
query.returnCountOnly = false;
query.outFields = ["OP"];
query.where = aSearch;

queryTask.execute(query, this.aShowResults);

....
aShowResults: function(results){
aCount = results.features.length;

if (aCount !== 0) {
aExtent = graphicsUtils.graphicsExtent(results.features);
}

addLayersToMap();
},

......

function addLayersToMap(){
map.addLayers([.......]);
map.setExtent(aExtent);
}

Your help is really appreciated, you sent me in the right direction!

0 Kudos