get the extent of a featureLayer

3794
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
1 Solution

Accepted Solutions
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 

View solution in original post

17 Replies
RobertScheitlin__GISP
MVP Esteemed Contributor

Sara,

   Something like this:

map.setExtent(layer17.fullExtent, true);
SaraEL_MALKI
Occasional Contributor II

It gives me an error, Uncaught TypeError: Cannot read property 'fullExtent' of undefined

0 Kudos
RobertScheitlin__GISP
MVP Esteemed Contributor

Then you featurelayer is not loaded yet, or you are just not getting the featurelayer based on the code you are using. Have you checked to see if layer17 is a valid FL?

0 Kudos
SaraEL_MALKI
Occasional Contributor II

It does not throw an error if I write :

var layer17 = map.getLayer("graphicsLayer17"); //I want to ZOOM to this Layer

but It does nothing, I want it to zoom to that extent with a 11 level !

0 Kudos
RobertScheitlin__GISP
MVP Esteemed Contributor

Yes that line may not through an error but have you checked the value of layer17?

console.log(layer17);

SaraEL_MALKI
Occasional Contributor II

in the console "

console.log(layer17.fullExtent);

it gives:

Map: Geometry (wkid: PROJCS["zone1",GEOGCS["GCS_Merchich_Degree",DATUM["D_Merchich",SPHEROID["Clarke_1880_IGN",6378249.2,293.4660212936265]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Lambert_Conformal_Conic"],PARAMETER["False_Easting",500000.0],PARAMETER["False_Northing",300000.0],PARAMETER["Central_Meridian",-5.4],PARAMETER["Standard_Parallel_1",34.866],PARAMETER["Standard_Parallel_2",31.725],PARAMETER["Scale_Factor",1.0],PARAMETER["Latitude_Of_Origin",33.3],UNIT["Meter",1.0]]) cannot be converted to spatial reference of the map (wkid: 102100)

0 Kudos
RobertScheitlin__GISP
MVP Esteemed Contributor

So that means that the layers WKID is different from the maps WKID of 102100 and you will have to project the layers full extent to 102100.

0 Kudos
SaraEL_MALKI
Occasional Contributor II

so how can I do that ? 

0 Kudos
SaraEL_MALKI
Occasional Contributor II

I found these lines: 

  var customExtentAndSR = new esri.geometry.Extent(-20037506,-9823074,20037506,10214431,new esri.SpatialReference({"wkid":54052}));
       
//create map with new custom spatial reference and extent.  The map will be in the Goode Homolosine Land projection
       
var map = new esri.Map("map", { extent: customExtentAndSR });

 How can use them for the extent of the layers ?

0 Kudos