Zoom to selected

2312
12
Jump to solution
04-14-2017 10:04 AM
GregRieck
Occasional Contributor III

Hello, I am using WAB 2.2 Jewelry box theme. I have features selected and would now like to zoom to those selected features. How do I zoom to selected features on the map?

Thank You,

Greg

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Greg,

   The selectFeatures is a deferred returning method so you just need to use a callback function like you do for any deferred.

cableLayer.selectFeatures(cableSelectQuery, cableLayer.SELECTION_NEW, lang.hitch(this, function(results){
  var selMgr = SelectionMgr.getInstance();
  var geom = selMgr.getUnionGeometryBySelectedFeatures(cableLayer);
  this.map.setExtent(geom.getExtent().expand(1.2), true);
}));

View solution in original post

12 Replies
RobertScheitlin__GISP
MVP Emeritus

Greg,

  There are multiple ways.

  1. Open the features popup and click the zoom to link
  2. Use the Attribute Table widget in your app and click the zoom to for the selected record.
  3. Use the selection widget results menu an zoom to(though that option does not zoom very close)
GregRieck
Occasional Contributor III

Hi Robert,

Thanks for the reply. I am looking for a solution that is code based. Something that will automatically zoom to the selected features as part of my widgets results from selecting features on the map. Using the feature popup only zooms to the feature in the popup. I have several features selected throughout the entire map extent. I do not want the user to have to open anything, click anything or do anything. Once the widget has completed it's server calls and feature selections I would like the selected features to be zoomed to on the map.

Greg

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Greg,

   OK, I did not get that you where talking about creating your own widget. For that you can add the jimu/SelectionManager to your widget and call the getUnionGeometryBySelectedFeatures method by passing in the layer you are interested in get back a unioned geometry of all the selected feature that you can get the extent from and set the maps extent to that extent to zoom to them.

0 Kudos
GregRieck
Occasional Contributor III

Robert,

Yes, I'm inside a custom widget. It has just done it's job and selected features on the map. I'd now like to zoom to those features.

I added jimu/SelectionManager to the define and function.

I then set it up like this:

var mylayer = this.map.webMapResponse.itemInfo.itemData.operationalLayers[3].layerObject;

var sm = SelectionMgr.getUnionGeometryBySelectedFeatures(mylayer);

this.map.spatialReference = sm;

And I get the following error: TypeError: SelectionMgr.getUnionGeometryBySelectedFeatures is not a function

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Greg,


  You need to do SelectionManger.getInstance()

0 Kudos
GregRieck
Occasional Contributor III

Robert,

Okay, thanks, I found the help on SelectionManager. But there is no information on getUnionGeometryBySelectedFeatures, where can I find help on that method? Am I supposed to return something that is used to set the extent?

I have this now:

var selMgr = SelectionMgr.getInstance();

var mylayer = this.map.webMapResponse.itemInfo.itemData.operationalLayers[3].layerObject;

selMgr .getUnionGeometryBySelectedFeatures(mylayer);

this.map.spatialReference = selMgr ;

No errors, but it doesn't zoom to the selected features. I checked and mylayer is a feature layer and is the correct one. I also looked and selMgr does have an array of objects

Greg

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Greg,

Yes the method returns a unioned geometry that you have to get the extent from and then set the maps extent using that.

var selMgr = SelectionMgr.getInstance();
var mylayer = this.map.webMapResponse.itemInfo.itemData.operationalLayers[3].layerObject;
var geom = selMgr.getUnionGeometryBySelectedFeatures(mylayer);
this.map.setExtent(geom.getExtent().expand(1.2), true);
GregRieck
Occasional Contributor III

Robert,

var geom = selMgr.getUnionGeometryBySelectedFeatures(mylayer);

So, now geom is null. selMgr is valid, myLayer is valid. Should I be looking at a specific value of mylayer to get the extent of all the selected features?

Greg

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Greg,

  If you do 

var selFeats = cableLayer.getSelectedFeatures();‍
console.info(selFeats);

Is the console output undefined, null, or an actual array of graphics?

The potential problem I see in your code when you are selecting from your featurelayer is the selectFeatures is an async process that returns a deferred and you are not waiting for the result before trying to get the selection:

cableLayer.selectFeatures(cableSelectQuery, cableLayer.SELECTION_NEW);
//You are not waiting for the results from this deferred returning method.