zoom to extent of a layer in a Map Service

9310
9
Jump to solution
06-01-2012 01:01 PM
SebastianRoberts
Occasional Contributor III
I thought this would be easy, but I can't figure out how to zoom to the extent of a layer in a map Service (not the extent of the entire map service).  In the Flex API there is a layerDetails class, but I can't find anything similar in JavaScript.
0 Kudos
1 Solution

Accepted Solutions
BenFousek
Occasional Contributor III
Check out http://dojotoolkit.org/reference-guide/1.7/dojo/io/script.html#dojo-io-script

All my apps are module based, pulling snippets and code from my server; consequently I make a ton xhrGet and xhrPost requests and have always used a proxy. I'm in the process of converting all my apps to dojo 1.7 in anticipation of jsapi v3, and will be doing more with io-script.

View solution in original post

0 Kudos
9 Replies
by Anonymous User
Not applicable
Are you referring to zooming into the extent of a specific layer within the MapService, for instance:

http://server.arcgisonline.com/ArcGIS/rest/services/Demographics/USA_Diversity_Index/MapServer/1

If so, you could just zoom into that particular layer's extent and not the map service's extent.  There is a DynamicLayerInfo class, too.  You could get the map service's sublayers and ultimately their extent.
0 Kudos
SebastianRoberts
Occasional Contributor III
Yes that is what I am referring to, but I am having trouble getting the extent of an individual layer.  I have an ArcGISDynamicServicLayer and I know the layer number of the layer that I want to zoom to, but I dont' know how to get its extent. 


By the way, I tried setting a variable equal to the dynamicLayerInfos property of my ArcGISDynamicServicLayer, but the resulting variable has a value of undefined.  However, even if I did get an array of LayerInfos, the LayerInfo class doesn't seem to have an extent property, so I don't know if that will helpful.

resultsLayer = new esri.layers.ArcGISDynamicMapServiceLayer(serviceURL, {id: "resultsLayer"});
        map.addLayer(resultsLayer);
layer = resultsLayer.dynamicLayerInfos 
0 Kudos
SebastianRoberts
Occasional Contributor III
I found a kludgy solution, but I know there must be a better way.  I create a featureLayer for the layer in the map service.  FeatureLayer has a fullExtent property.  However, the fullExtent property doesn't exist immediately after the featureLayer is first created.  If I do a selection on the Layer, then the property exists.  Therefore, I do a selection (using a where clause that I know won't select any features), and in the callback I can zoom to the layer.

     zoomLayer = new esri.layers.FeatureLayer(serviceURL+layernum);
     var query = new esri.tasks.Query();
     query.where = "0=1";
     zoomLayer .selectFeatures(query,esri.layers.FeatureLayer.SELECTION_NEW,zoomSel);
}
.........

function zoomSel(){
        map.setExtent(zoomLayer.fullExtent);
}
0 Kudos
BenFousek
Occasional Contributor III
Individual layers in a map service have an extent but I don't see where dynamic layer creates an object for for it. You could always make a rest call to the layer and get it's extent:

var getLayerExtent = dojo.xhrGet({
   url: 'http://MY_URL/ArcGIS/rest/services/MY_MAP_SERVICE/MapServer/0?f=json',
   handleAs: 'json',
   load: function(r) {
       var extent = r.extent;
       //zoom to it
   }
);
0 Kudos
JohnnyPenet
New Contributor
Did you try to get the information when the layer has been loaded? You can do this in the following way :

                mapLayer = new esri.layers.ArcGISDynamicMapServiceLayer(layerUrl,
    { id: layerName,
        visible: visible
    });
                handle = dojo.connect(mapLayer,"onLoad", function (layer) {
                    dojo.disconnect(handle);
                    ... Do the extent processing here
                });
0 Kudos
BenFousek
Occasional Contributor III
When you add a dynamic layer the api makes a rest call to the map service (http://MY_URL/ArcGIS/rest/services/MY_MAP_SERVICE/MapServer?f=json) which includes sub-layer information but not detailed sub-layer info like extent. The only way to get a sub-layer's extent to to make some some kind of rest call on said sub-layer (http://MY_URL/ArcGIS/rest/services/MY_MAP_SERVICE/MapServer/0?f=json). Creating a feature layer is one way, but a lot of work just to get the extent.
0 Kudos
SebastianRoberts
Occasional Contributor III
I think Ben is right,  you can't get the extent of sublayers from the ArcGISDynamicMapServiceLayer object.  Ben, your solution is much more elegant than mine, but when I tried it I ran into crossdomain issues when trying to make the dojo.xhrGet call since my webserver is not the same as my ArcGIS server. It seems I would need to set up a proxy on my webserver in order to make the rest call, but somehow the ArcGIS JavaScript api gets around this and makes rest calls.
0 Kudos
BenFousek
Occasional Contributor III
Check out http://dojotoolkit.org/reference-guide/1.7/dojo/io/script.html#dojo-io-script

All my apps are module based, pulling snippets and code from my server; consequently I make a ton xhrGet and xhrPost requests and have always used a proxy. I'm in the process of converting all my apps to dojo 1.7 in anticipation of jsapi v3, and will be doing more with io-script.
0 Kudos
SebastianRoberts
Occasional Contributor III
Ben, Thanks for your help. Using dojo.script.io (JSONP) worked.  Below is the resultant code.  For some reason I couldn't use the data.extent object directly, but had to create a new object.

dojo.io.script.get({
    url: queryURL+layernum+'?f=json',
    content: {q: "#dojo"},
    callbackParamName: "callback"
}).then(function(data){   
      var newExtent = new esri.geometry.Extent
      newExtent.xmax = data.extent.xmax;
      newExtent.xmin = data.extent.xmin;
      newExtent.ymax = data.extent.ymax;
      newExtent.ymin = data.extent.ymin;
      map.setExtent(newExtent);
   });
}
0 Kudos