featureLayer.selectFeatures(query, esri.layers.FeatureLayer.SELECTION_NEW, function (features) { var extent = features[0].geometry.getExtent().expand(1.25); map.setExtent(extent, true); }
myLayer.on('selection-complete', selectFeatureHandler); myLayer.selectFeatures(query); function selectFeatureHandler (results) { var feature = results.features[0]; map.setExtent(feature.geometry.getExtent().expand(1.25)); }
I occasionally have problems with both selecting and querying and I always think it relates to the callback function executing before the selection is really complete. Network connectivity, the type and number of features you're selecting can impact whether the selection happens 'instantly' or not. Points seem to be OK, but when I had a project where I was selecting lines with lots of vertices, there was often a problem.
When I have this happen, adding selection-complete listener to your featureLayer and then execute the selection without the callback function takes care of this problem. The syntax changes up a bit. Instead of getting to feature like "results[0]" you have to use "results.features[0]".myLayer.on('selection-complete', selectFeatureHandler); myLayer.selectFeatures(query); function selectFeatureHandler (results) { var feature = results.features[0]; map.setExtent(feature.geometry.getExtent().expand(1.25)); }