Select to view content in your preferred language

Zoom to extent of graphic array in Feature Layer not working

5793
4
06-30-2011 09:11 PM
GeorgieCassar
Frequent Contributor
I can't work out why my map is not zooming to the extent of graphics in my featurelayer.

See at the bottom of this code where I try and use the esri.graphicextent method.

function initOperationalLayer(map) {
        var content = "<b>Street: </b>${Street_Name}, ${Suburb}<br/>"
                             + "<b>Pickup Time: </b>${Departure_Time}";

        var infoTemplate = new esri.InfoTemplate("Route 1 Details", content);

        var featureLayer = new esri.layers.FeatureLayer("http://gsportenf/ArcGIS/rest/services/Public_Access/MapServer/12",{
          mode: esri.layers.FeatureLayer.MODE_ONDEMAND,
          outFields: ["*"],
          infoTemplate: infoTemplate
        });


        map.addLayer(featureLayer);
        var symbol = new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([255,255,255,0.35]), 1);
        var renderer = new esri.renderer.SimpleRenderer(symbol);
        featureLayer.setRenderer(renderer);
        map.infoWindow.resize(300,100);
        var zoomExtent= esri.graphicsExtent(featureLayer.graphics);
        map.setExtent(zoomExtent);    // THE MAP IS NOT CHANGING !!!
  }
0 Kudos
4 Replies
HemingZhu
Frequent Contributor
I can't work out why my map is not zooming to the extent of graphics in my featurelayer.

See at the bottom of this code where I try and use the esri.graphicextent method.

function initOperationalLayer(map) {
        var content = "<b>Street: </b>${Street_Name}, ${Suburb}<br/>"
                             + "<b>Pickup Time: </b>${Departure_Time}";

        var infoTemplate = new esri.InfoTemplate("Route 1 Details", content);

        var featureLayer = new esri.layers.FeatureLayer("http://gsportenf/ArcGIS/rest/services/Public_Access/MapServer/12",{
          mode: esri.layers.FeatureLayer.MODE_ONDEMAND,
          outFields: ["*"],
          infoTemplate: infoTemplate
        });


        map.addLayer(featureLayer);
        var symbol = new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([255,255,255,0.35]), 1);
        var renderer = new esri.renderer.SimpleRenderer(symbol);
        featureLayer.setRenderer(renderer);
        map.infoWindow.resize(300,100);
        var zoomExtent= esri.graphicsExtent(featureLayer.graphics);
        map.setExtent(zoomExtent);    // THE MAP IS NOT CHANGING !!!
  }


To my understanding, you set up featurelayer displayer mode as MODE_ONDEMAND. Without definition expression and time definition, the feature layer only display features at current map extent. So featureLayer.graphics in your statement actually are not the whole features in your featureLayer but the features in your current extent. So zoomExtent =YOUR CURRENT EXTENT !!. No wonder your map is not changing.
0 Kudos
GeorgieCassar
Frequent Contributor
I found out with an alert that my featurelayer.graphics.length was = 0 so I could not even access the graphics at all.
I found a post titled "unable to draw graphic" which helped me solve this problem.

If the layer isn't finished drawing before I try and zoom to the graphic extent, it simply won't zoom.

What I did was move the zoom code to an event handler using the onUpdateEnd event of the feature layer.

// Just after defining the featurelayer >

dojo.connect(featureLayer, "onUpdateEnd", function() {
var zoomExtent = esri.graphicsExtent(featureLayer.graphics);
map.setExtent(zoomExtent);
});     //  now the zoom won't happen until map is updated
map.addLayer(featureLayer);
0 Kudos
RoseZhou
Deactivated User
I found out with an alert that my featurelayer.graphics.length was = 0 so I could not even access the graphics at all.
I found a post titled "unable to draw graphic" which helped me solve this problem.

If the layer isn't finished drawing before I try and zoom to the graphic extent, it simply won't zoom.

What I did was move the zoom code to an event handler using the onUpdateEnd event of the feature layer.

// Just after defining the featurelayer >

dojo.connect(featureLayer, "onUpdateEnd", function() {
var zoomExtent = esri.graphicsExtent(featureLayer.graphics);
map.setExtent(zoomExtent);
});     //  now the zoom won't happen until map is updated
map.addLayer(featureLayer);


But this event keeps firing... If you add an alert in this event handler, you will know what I mean. How to fix this problem?

Thanks
0 Kudos
HunterWei
Deactivated User
But this event keeps firing... If you add an alert in this event handler, you will know what I mean. How to fix this problem?

Thanks


I met the same issue, but finally I figured it out with a turnaournd approach by using a featurelayer query, see below code

featureLayer.setDefinitionExpression("COUNTY='" + value + "'");

var query = new esri.tasks.Query();
query.where = "1=1";
query.outSpatialReference = map.spatialReference;
featureLayer.queryFeatures(query, function (featureSet) {
    var data = [];
    if (featureSet && featureSet.features && featureSet.features.length > 0) {
        data = featureSet.features;
    }
    var zoomExtent = esri.graphicsExtent(data);
    map.setExtent(zoomExtent);
});
0 Kudos