Query Feature and Zoom

3004
12
01-06-2012 02:05 PM
CarlSunderman1
New Contributor II
I've searched here and the rest forums and can't seem to find an answer.

I have a php page that when a value is click, a fancybox iframe opens with data, a floor plan, and a map of the building. i have passed the variable to javascript but am unable to see how to pass the variable to the map to zoom to the feature(QueryTask?). i've tried using query?where= but i seem to be way off. i've looked into the QueryTask, but have been unable to find an example of what i'm trying to accomplish. Does anyone have an example i can look at?
0 Kudos
12 Replies
StephenLead
Regular Contributor III
i have passed the variable to javascript but am unable to see how to pass the variable to the map to zoom to the feature(QueryTask?)


Hi Carl,

Can you provide further information - which variable are you passing to the map? Have you configured a query and querytask?

Can you provide a link to your site, or some sample code?

Steve
0 Kudos
CarlSunderman1
New Contributor II
Unfortunately, our server is on our intranet. I'm passing the objectid value to js Is a query task really needed? there is no other way to pass the value?  the query tasks i have seen seem to be more related to the data table and i can't seem to find a simple example of passing a variable to the query task to pan and zoom

i did find this:
function zoomRow(id){
      selectionLayer.clear();
      dojo.some(map.graphics.graphics,function(graphic){
        if (graphic.attributes.ObjectID.toString() === id) {
          var selectedState = new esri.Graphic(graphic.geometry).setAttributes(
              graphic.attributes);
          selectionLayer.add(selectedState);
          var stateExtent = selectedState.geometry.getExtent().expand(5.0);
          map.setExtent(stateExtent);
          return true;
        }
      });

here http://blogs.esri.com/Dev/blogs/arcgisserver/archive/2009/12/14/Add-a-Zoom-button-to-a-DataGrid.aspx

which basically does what i want, i just need to do an onload, i think
0 Kudos
StephenLead
Regular Contributor III
i can't seem to find a simple example of passing a variable to the query task to pan and zoom


The sample you referenced may not be perfectly suited to your task - it's passing in the OBJECTID of the feature, then iterating through each graphic on the map to see whether its ID matches. It then zooms to the matching graphic.

Another approach is to run a query, then zoom to the extent of the feature(s) which are returned. Assuming that you know the OBJECTID (or some other unique attribute) of your feature, you could use something like this:

//Define a new Query and QueryTask
queryTask = new esri.tasks.QueryTask(UrlToYourLayer);
query = new esri.tasks.Query();
query.returnGeometry = true;
query.outFields = ["field1","field2"];


//The query's where clause will be attribute query to find the feature
query.where = "'OBJECTID = 123'";


//Run the query. Its results are returned in the function showResults
queryTask.execute(query,showResults);


//results is an array of any found features. Assuming you only want the first feature:
function showResults(results) {
    var result = results[0];
    var extent = result.geometry.getExtent().expand(5.0);
    map.setExtent(extent);
}


(Note that I haven't actually tested this so it's more of a conceptual overview.)

Cheers,
Steve
0 Kudos
CarlSunderman1
New Contributor II
Thanks stevel

With outfields populated i get a
"dojo.io.script.jsonp_dojoIoScript1._jsonpCallback({"error":{"code":400,"message":"Unable to complete  operation.","details":["Unable to complete Query operation."]}});" error

with outfields empty i get
"TypeError: Cannot read property 'geometry' of undefined {}"

regardless, the map errors and doesn't draw

<script type="text/javascript">
  dojo.require("esri.map");
  dojo.require("esri.tasks.query");
   
   function init() {
    map = new esri.Map("mapDiv");
    var basemapURL= "http://<myserver>/ArcGIS/rest/services/<myservice>/MapServer"
    var basemap = new esri.layers.ArcGISDynamicMapServiceLayer(basemapURL);
    map.addLayer(basemap);
   
    // sample Query Task
    // define variables
     var queryTask, query;
    //Define a new Query and QueryTask
    queryTask = new esri.tasks.QueryTask("http://<myserver>/ArcGIS/rest/services/<myservice>/MapServer/7");
    query = new esri.tasks.Query();
    query.returnGeometry = true;
    query.outFields = ["BUILDNG_NO","ADDRESS_ID"];


//The query's where clause will be attribute query to find the feature
query.where = "OBJECTID=6742";


//Run the query. Its results are returned in the function showResults
queryTask.execute(query,showResults);


//results is an array of any found features. Assuming you only want the first feature:
   function showResults(results) {
    var result = results[0];
    var extent = result.geometry.getExtent().expand(5.0);
    map.setExtent(extent);
   
   }
    // end of Query Task
     }

  dojo.addOnLoad(init);
</script>
0 Kudos
StephenLead
Regular Contributor III
You know how I said I hadn't actually tested that code? There were a couple of typos in there. The showResults section should look something like this:

function showResults(results) {
  var result = results.features[0];
  var extent = result.geometry.getExtent().expand(5.0);
  map.setExtent(esri.geometry.geographicToWebMercator(extent));
}


The key missing thing was results.features[0], to obtain the first feature in the results.

You may also need to translate the extent from geographic coordinates to web mercator, depending on your datasets (as in the attached demo, which zooms to the extent of a polygon on opening. That's because the query uses a dataset specified in decimal degrees, while the basemap is in Web Mercator.)

Cheers,
Steve
0 Kudos
CarlSunderman1
New Contributor II
Stevel

  thanks, That's exactly what i needed

Carl
0 Kudos
StephenLead
Regular Contributor III
Cool, glad you got it sorted.

The first sample was pretty dodgy - it loaded the map then immediately ran the query, which isn't very useful.

In case anyone reads this thread later, attached is a more useable version. It waits for the user to enter a query string, then runs the query when the user hits the Query button.

If you step through it in FireBug you can see what's happening.

Cheers,
Steve
0 Kudos
ChristopherPollard
Occasional Contributor
Steve,
Thanks so much for the query sample code.
I have a mapping application that I need to zoom to ALL the features returned, not just the 1st feature.
I tried to modify your sample but was not getting anywhere.
Any help would be appreciated.
Thanks,
Chris
0 Kudos
StephenLead
Regular Contributor III
I need to zoom to ALL the features returned, not just the 1st feature. I tried to modify your sample but was not getting anywhere.


See the section:

function showResults(results) {
  //This function is run when the query has finished.
  //It zooms the map to the extent of the first feature returned by the query.
  var result = results.features[0];
  var extent = result.geometry.getExtent().expand(5.0); 


You'll want to iterate through each feature and union the extent so that it expands to include all features. Pseudo-code is:

var fullExtent;
for each result in results:
  thisExtent = result.geometry.getExtent();
  fullExtent.union(fullExtent);


I haven't tested this, so you might need to initialise fullExtent as the extent of the first feature, before union-ing it with all of the other features' extents.

Good luck,
Steve
0 Kudos