<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=7, IE=9, IE=10"> <!--The viewport meta tag is used to improve the presentation and behavior of the samples on iOS devices--> <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"> <link rel="stylesheet" href="http://serverapi.arcgisonline.com/jsapi/arcgis/3.5/js/dojo/dijit/themes/claro/claro.css"> <link rel="stylesheet" href="http://serverapi.arcgisonline.com/jsapi/arcgis/3.5/js/esri/css/esri.css"> <title>Query State Info with Map</title> <script src="http://serverapi.arcgisonline.com/jsapi/arcgis/3.5/"></script> <script> dojo.require("esri.tasks.query"); dojo.require("esri.map"); var queryTask, query, centerPoint, stateName; var stateName = "Colorado" /* stateName for QueryTask */ function init() { /* Setup query */ queryTask = new esri.tasks.QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5"); query = new esri.tasks.Query(); query.returnGeometry = true; query.outSpatialReference = new esri.SpatialReference({wkid : 102100}); query.outFields = ["STATE_NAME","SQMI","SUB_REGION"]; query.text = stateName; queryTask.execute(query, showResults); } function showResults(results) { var s = ""; for (var i=0, il=results.features.length; i<il; i++) { var featureAttributes = results.features.attributes; for (att in featureAttributes) { s = s + "<b>" + att + ":</b> " + featureAttributes[att] + "<br />"; } } dojo.byId("info").innerHTML = s; /* Extract Geometry From Query */ centerPoint = new esri.geometry.Point(getPoint(results)); //Does NOT work- Actual use will be a point layer // centerPoint = [-122.45, 37.75] //fake point for testing /* Build Map */ map = new esri.Map("map",{ basemap: "topo", center:centerPoint, zoom:13 }); map.addLayer(new esri.layers.ArcGISDynamicMapServiceLayer("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer", { "opacity":0.2, spatialReference: map.spatialReference })); /* Add ESRI Census Map */ } dojo.ready(init); </script> </head> <body class="claro"> <div id="info" style="padding:5px; margin:5px; background-color:#eee;"></div> <div id="map"></div> </body> </html>
Solved! Go to Solution.
function showResults(results) { // This takes the graphics array you get back from your query and // gets the overall extent for them. Make sure return geometry is set to // true in your query. var extent = esri.graphicsExtent(results.features); // Use that to set the extent, 1.5 is something I use in my app, but play with // it to find a setting you like, setting the second parameter to true will get you an extend // that is at the closest level of your cached service. map.setExtent(extent.expand(1.5), true); }
map = new esri.Map("map",{ basemap: "topo", extent: extent })
function showResults(results) { // This takes the graphics array you get back from your query and // gets the overall extent for them. Make sure return geometry is set to // true in your query. var extent = esri.graphicsExtent(results.features); // Use that to set the extent, 1.5 is something I use in my app, but play with // it to find a setting you like, setting the second parameter to true will get you an extend // that is at the closest level of your cached service. map.setExtent(extent.expand(1.5), true); }
map = new esri.Map("map",{ basemap: "topo", extent: extent })
var extent = esri.graphicsExtent(results);anywhere in the "showResults" function, the map fails- even if I don't use extent anywhere else.
var extent = esri.graphicsExtent(results.features);
//features[0] will always be the first item in an array var extent = results.features[0].geometry.getExtent(); map = new esri.Map("map",{ basemap: "topo", extent: extent });
//in order to call the getPoint method of a polygon geometry object, you have to pass index positions to articulate which ring and specific point you want centerPoint = results.features[0].geometry.getPoint(0,0); map = new esri.Map("map",{ basemap: "topo", center:centerPoint, zoom:13 });