You're right that the layer isn't fully loaded. There's an event that tells you when layers are loaded :
onLayerAddResult(layer, error)
As of v2.0 it triggers after specified layer has been added to the map.
OR
onLayersAddResult(results)
Which triggers after all layers are added to the map using the map.addLayers
method.
http://developers.arcgis.com/en/javascript/jsapi/map.html
Maybe better to use that rather than setting a timeout in your application since it's impossible to know exactly how long it will take to load.
You could do this in different ways, but the basics are connecting the map click event, finding what was clicked and then either zooming to the extent of the polygon (easiest) or creating a new map on top of the old map (or it could be below or to the side or a new page) -- you'll have to play around with the div where you define another map.
I would keep it simple to start and just zoom in to the detail layers. Set scale dependencies in your map to display details only at the zoomed in levels.
I'm using a feature layer below in selection mode. You could just use a ArcGISDynamicMapServiceLayer or Tiled if you want.
Where I am using subblock, substitute any polygon layer such as US states.
//Define a feature layer
var subblockQueryFeatureLayer = new esri.layers.FeatureLayer("http://server/featurelayername", {
id: 'subblockQueryFeatureLayer',
mode: esri.layers.FeatureLayer.MODE_SELECTION,
outFields: ["*"]
});
// Define the selection symbol for feature layer
var outlineColor = new dojo.Color(dojo.Color.named.red.concat(0.8));
var outlineSym = new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, outlineColor, 3.0);
var selectionColor = new dojo.Color(dojo.Color.named.red.concat(0.35));
var selectionSymSubblocks = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID, outlineSym, selectionColor);
//define mapclick event
var mapClickEvent
//connect map click event
function activateSubblockMapClick() {
if (!mapClickEvent) {
mapClickEvent = dojo.connect(map, "onClick", mapOnClick);
}
subblockQueryFeatureLayer.setSelectionSymbol(selectionSymSubblocks);
}
//disconnect map click event
function deactivateSubblockMapClick() {
if (mapClickEvent) {
dojo.disconnect(mapClickEvent);
mapClickEvent = null;
}
}
//do something when the click occurs
function mapOnClick(evt) {
subblockQueryFeatureLayer.clearSelection();
var selectQuery = new esri.tasks.Query();
selectQuery.geometry = evt.mapPoint;
selectQuery.outFields = ["*"];
subblockQueryFeatureLayer.selectFeatures(selectQuery, esri.layers.FeatureLayer.SELECTION_NEW, function (features) {
//after highlighting feature (or not), zoom to feature to show detailed map
if (features !== undefined && features.length !== 0) {
map.setExtent(features[0].geometry.getExtent().expand(10), true);
}
});
}
Check out this identifytask snippet for querying scale dependent layers http://www.spatialexception.org/posts/arcgis-javascript-identifytask-returns-scale-dependent-layers
Best Answer
You can do this using setFeatures() and show() on the map's infoWindow (which by default is an instance of esri/dijit/Popup). You'll want to call clearFeatures() and hide() before calling setFeatures and show. I also recommend waiting until the map finishes navigating to the new center before calling setFeatures and show. This is easy since centerAt() returns a deferred so you can use its then() method to ensure the map is centered before you open the popup.
Here's how you would update the selectState function to do this:
Full working example: http://jsbin.com/UXaYidi/1/edit