Zooming into layers from a dynamic map service

2090
9
Jump to solution
09-09-2013 07:08 AM
BetsySchenck-Gardner
Occasional Contributor
Is there any way to zoom into some of the layers of a dynamic map service?  I'm doing this:
var imageParameters = new esri.layers.ImageParameters(); imageParameters.layerIds = [2,3,4]; imageParameters.layerOption = esri.layers.ImageParameters.LAYER_OPTION_SHOW; GISLayer = new esri.layers.ArcGISDynamicMapServiceLayer(mapServiceURL,{"imageParameters":imageParameters}); map.addLayer(GISLayer);

I get the layers I want to see on the map but I would like to have map zoom into the extent of these layers. Is this possible?
TIA
0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor
If you're trying to get the extent of the features within that particular layer, you can use the QueryTask to return all the features and set the extent from those features

queryTask = new esri.tasks.QueryTask("http://service.ncddc.noaa.gov/arcgis/rest/services/OceanExploration/regatlasSATL/MapServer/5"); query = new esri.tasks.Query(); query.returnGeometry = true; query.outFields = ["zkdiveid"]; query.where = "1=1";  queryTask.execute(query, function (fset) {      map.setExtent(esri.graphicsExtent(fset.features)); });

View solution in original post

0 Kudos
9 Replies
JasonZou
Occasional Contributor III
Here is a sample code to zoom to one layer's extent. If you need to zoom to the extent of multiple layers, you will need to get the extent of each layer using esri.request, and use geometryService.union to get the extent to cover all the layers, and zoom the map to it.

esri.request({
    url: mapService.url + "/" + lyrId,
    content: {
        f: "json"
    },
    handleAs: "json",
    callbackParamName: "callback"
}).then(function (jsonLayer) {
    // NOTE: jsonLayer.extent is not a real Extent object, but just a json object
    map.setExtent(new esri.geometry.Extent(jsonLayer.extent));
});
0 Kudos
KenBuja
MVP Esteemed Contributor
Jason,

Instead of using a geometry service to union the extents together, why not just use the Extent's Union method (assuming the extents have the same spatial reference)
0 Kudos
JasonZou
Occasional Contributor III
Good point, Ken! This is another way to union the extents one by one, and it should perform on the client side for a little performance gain.
0 Kudos
BetsySchenck-Gardner
Occasional Contributor
Thanks guys. The method works but unfortunately not the way I need it to. The problem I have is we have general layers for a specific type of data ... say ship tracks or dive locations. But these can be world-wide. In the map service, they are using a layer definition call to subsample out the points or lines for a particular cruise but the layer extent is for the whole general layer. So when I use this routine, it zooms to the extent of the general layer ... which is what you expect ... and not to the smaller subset.
0 Kudos
KenBuja
MVP Esteemed Contributor
You can use esri.graphicsExtent to get the extent from an array of graphics. Just be warned that it won't work as expected if there is only one point in the graphics array.
0 Kudos
BetsySchenck-Gardner
Occasional Contributor
So I created a jsfiddle example to show my results of trying to get the extent to work. None is coming back with the results I want. First setting uses map.setExtent(map.extent), second setting uses esri.request call, and third setting uses esri.graphicsExtent. Can you tell me what I'm doing wrong? Here's the link to jsfiddle example: http://jsfiddle.net/betsyjsg/8Ty9x/7/
0 Kudos
VinayBansal
Occasional Contributor II
I think you need something like this......

 GISLayer = new esri.layers.ArcGISDynamicMapServiceLayer("http://service.ncddc.noaa.gov/arcgis/rest/services/OceanExploration/regatlasSATL/MapServer", { "imageParameters": imageParameters });
    map.addLayer(GISLayer);
   dojo.connect(GISLayer, 'onLoad', function(mapLayout) {
      map.setExtent(GISLayer.fullExtent);
    });

    //1. set map extent example
    // map.setExtent(map.extent);
0 Kudos
KenBuja
MVP Esteemed Contributor
If you're trying to get the extent of the features within that particular layer, you can use the QueryTask to return all the features and set the extent from those features

queryTask = new esri.tasks.QueryTask("http://service.ncddc.noaa.gov/arcgis/rest/services/OceanExploration/regatlasSATL/MapServer/5"); query = new esri.tasks.Query(); query.returnGeometry = true; query.outFields = ["zkdiveid"]; query.where = "1=1";  queryTask.execute(query, function (fset) {      map.setExtent(esri.graphicsExtent(fset.features)); });
0 Kudos
BetsySchenck-Gardner
Occasional Contributor
Many thanks Ken. That was the solution that worked for me.
0 Kudos