Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to zoom to layer automatically as soon as the map finishes loading using the guide here. To be clear, I'm only loading one layer and just want to zoom in on one of the layers provided by the service.

js script



var map;
var basemap;
var trafficmap;
var basemapServiceSource;
var featuremapServiceSource;
var trafficServiceSource;
var renderer;
var loading;

function init() {

    // for now, these are referring to a single source
    basemapServiceSource = "http://co-gis-01/ArcGIS/rest/services/dev_RTIA/rtia/MapServer";

    //Add the topographic layer to the map. View the ArcGIS Online site for services http://arcgisonline/home/search.html?t=content&f=typekeywords:service          
    basemap = new esri.layers.ArcGISDynamicMapServiceLayer(basemapServiceSource);
    basemap.setVisibleLayers([0, 1, 2, 4, 5, 6]);

    map = new esri.Map("map", {

            // remove logo

    //loading image. id
    loading = dojo.byId("loadingImg");

        IDs of Layers for http://co-gis-01/ArcGIS/rest/services/dev_ExIS/exis/MapServer
            Site Name (0)
            Section ID (1)
            AADT some (2)
            AADT all (3)
            AdministrativeAreas_CongressionalDistricts (4)
            AdministrativeAreas_Provinces (5)
            AdministrativeAreas_Regions (6)

    // executes function on onLoad event
    dojo.connect(map, 'onLoad', function(theMap) { 

        dojo.connect(dijit.byId('map'), 'resize', map,map.resize);

function zoomToLayer() {

    var requestHandle = esri.request({
        url: "http://co-gis-01/ArcGIS/rest/services/dev_RTIA/rtia/MapServer/2",
        content: { f:"json" },
        callbackParamName: "callback"

    requestHandle.then(requestSucceeded, requestFailed);

function requestSucceeded(response, io) {

    var extent = new esri.geometry.Extent(response.extent);
    dojo.byId("extent").innerHTML = dojo.toJson(extent.toJson());

function requestFailed()    {


function showLoading()  {;

function hideLoading(error) {



html code

    <title>RTIA Map</title>
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" >

    <link rel="stylesheet" type="text/css" href="gis.css" />

    <script type="text/javascript" src=""></script>
    <script type="text/javascript" src="rtia_culled.js"></script>



    <div id="map" class="mainmap">
        <img id="loadingImg" src="images/loading.gif" class="loadimage"/>
    <!--div id="legendDiv" style="width:600px; height:600px; border:1px solid #000"-->

    <span id="extent"></span>


The problem is that the outputs of dojo.toJson(extent.toJson()) show the same extent, which is the full extent, no matter the layer. This particular layer that I would like to zoom to, http://co-gis-01/ArcGIS/rest/service...ia/MapServer/2, is just a filtered version of one of the "full-extent" layers. The filter is defined in the mapservice.

How do I correctly do this function?

share|improve this question
I tried setting only the layer of interest as visible, and then setting the map extent to full extent. Still, only the whole full extent is displayed instead of the extent that can only be shown by the layer. – NeoJigglypuff Jun 28 '12 at 1:45
I tried exporting the filtered layer and importing it, resulting in a layer whose content is only those originally defined by the definition query. I published the service and was finally able to automatically zoom to the desired extent. The definition query seems to be ignored when getting the full extent of the layer, in other words, the full extent still considers all features, even those that fails the definition query. is there a way to circumvent this in the api? – NeoJigglypuff Jun 28 '12 at 6:05
up vote 4 down vote accepted

I've managed to solve this problem using this as a reference. Basically, I looped all of the Polyline graphics (that's the only type the application is interested in) included in the FeatureLayer and do a union operation on each of the graphic's extent. Below is my version.

function featureUpdateEnd(error, info)  {

    var localExtent;
    if ( > 0) {

        for (var i = 0; i <; i++)    {

            if ([i].geometry instanceof esri.geometry.Polyline)  {

                try {

                    if (i == 0) {

                        localExtent = new esri.geometry.Extent([i].geometry.getExtent().toJson());
                    else    {

                        localExtent = localExtent.union([i].geometry.getExtent());
                catch (err) {

                    //alert(String(i) + " is caught: " + err.message);

        localExtent = localExtent.expand(1.25);

featuremap is the FeatureLayer. featureUpdateEnd is a function that is called by FeatureLayer's onUpdateEnd event. I did not use onLoad because the event fires even if the FeatureLayer hasn't finished loading yet, giving me a zero-length array to loop on. I had to enclose part of it in a try-catch statement because of an undefined instance is encountered (but this is probably only due to my data).

share|improve this answer
@Sam007. I Want to show concurrently all the features of various map layers (featureLayer, graphicsLayers , etc). Would I have to iterate over all the graphics so as to find a proper extent ? isnt there a more clever way to tell the map to zoom automatically to all the visible/loaded features ? – Smalis Dec 30 '14 at 18:51

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.