get the extent of a featureLayer

5880
17
Jump to solution
05-01-2017 09:11 AM
SaraEL_MALKI
Occasional Contributor II

Hi guys,

I want to get the extent of a featureLayer and Zoom to it, How I can Implement that in my code ?

function HideAllLayers() {
var layerIds = map.graphicsLayerIds;
var layer17 = map.getLayer(17); //I want to ZOOM to this Layer
var layer;
for (var i=0; i < layerIds.length; i++) {
layer = map.getLayer(layerIds[i]);
layer.hide();
}
$('#editModal').modal('hide'); //CLOSE THE MODAL
}
0 Kudos
17 Replies
SaraEL_MALKI
Occasional Contributor II

Okay I will create a geometry Service and keep you in touch with what I've done,

thanks for your time

0 Kudos
KristianEkenes
Esri Regular Contributor

Since layer.fullExtent isn't totally reliable, I suggest doing the following:

featureLayer.on("load", function(){
  var query = new Query();
  query.where = "1=1";
  query.outSpatialReference = new SpatialReference(3857);
      
  featureLayer.queryExtent(query, zoomToExtent);
});
    
function zoomToExtent(response){
  var extent = response.extent;
  map.setExtent(extent, true);
}

That way you don't have to bring in GeometryService and reproject geometries. Set the outSpatialReference to match the map and the resulting extent should be good. Here's a live demo of this as well: JS Bin - Collaborative JavaScript Debugging 

SaraEL_MALKI
Occasional Contributor II

Thanks so much for the live Demo, How can I add a zoom level in that Code ? 

0 Kudos
KristianEkenes
Esri Regular Contributor

How do you plan to use the zoom level?

SaraEL_MALKI
Occasional Contributor II

I wanna zoom to that extent with a 10 level

0 Kudos
KristianEkenes
Esri Regular Contributor

After setting the map extent call: 

map.setZoom(10);

SaraEL_MALKI
Occasional Contributor II

Thank you Kristian for your time

0 Kudos