jordan_clark

map.setExtent not working as expected

Discussion created by jordan_clark on Nov 21, 2013
Latest reply on Dec 5, 2013 by btfou
Overview / Expected Result:

I have a combo box that once an item is selected selected will add a feature layer to a map. I would like the map to 'zoom' to the feature layer boundary so you can see the entire layer. These are boundaries of rivers, essentially. Even if I am zoomed in at level 6 I should be able to select another feature layer and have the map zoom out and center to the larger layer even if it isn't currently viewable when it is selected.

The Problem:

The feature layers are sometimes as large as an entire state but sometimes only part of a coastal island. In the case of the coastal island, the map zooms to the extent of that small layer. If after I am zoomed into the small layer and I want to next select a feature layer that might be an entire state and is not viewable in the current view I get this error:
TypeError: Unable to get value of the property 'geometry': object is null or undefined
However, if I manually zoom the map out enough, as soon as the layer is visible on the map it then tries to set the extent to that layer.

My Code:
I have tried two ways of accomplishing this.

1.) Add the feature layer to the map once, then apply a definition expression to only show the parts I want.

2.) Run map.removeAllLayers(); map.setBasemap("topo"); and re-add the layer every time.

Both ways produce the same error. Now the actual code (removing and re-adding every time):

try {
            var temp = new Array();
            $.each(data.features, function (k, v) {
                temp[k] = "'" + v.attributes['SUBPROJ_ID'] + "'";
                options += '<option value="' + v.attributes['SUBPROJ_ID'] + '">' + v.attributes['PROJECT_NAME'] + '</option>';
            })

            projIds = temp.join();

            map.removeAllLayers();
            map.setBasemap("topo");

            var featureLayer = new FeatureLayer("http://<srvaddress>/arcgis/rest/services/Environment/Projects/MapServer/1", {
                mode: FeatureLayer.MODE_ONDEMAND,
                outFields: ["*"],
                id: "chanLayer"
            });

            featureLayer.setDefinitionExpression("CHANNEL_ID IN(" + projIds + ")");

            map.addLayer(featureLayer);

            mapSetExtent(featureLayer);

} catch (e) {
            console.log('(showResults) script prob. ' + e);
        }

function mapSetExtent(layer) {
        var layerZoomToExtent = layer.on("update-end", function () {
            try {
                console.log('layer update end');
                
                map.setExtent(esri.graphicsExtent(layer.graphics).expand(1.5));
                layerZoomToExtent.remove();
            } catch (e) {
                console.log('(mapSetExtent) script prob. ' + e);
            }
        })
    }


Does anyone have any ideas on a work around or spot anything I am doing wrong? Many thanks in advance.

Outcomes