Select to view content in your preferred language

Search Results in Data Grid then zoom to feature (point) Problem

770
5
08-06-2013 06:47 AM
IanPeebles
Frequent Contributor
I have set up a search function that allows a user to type in a value then perform a search for a point feature.  Results will post in a data grid.  When I click on the record in the data grid I can zoom to a point.  However, when I click another record to zoom to that record, the map zooms in further.  Is there a way to prevent this from happening?  I need to keep the same zoom extent for all features, rather than zooming in further as each record is clicked.  Here is the code that I have so far:

Does not include variable declaration or function init (


Code:

function doFind() {
        //Set the search text to the value in the box
        findParams.searchText = dojo.byId("searchText").value;
  if (findParams.searchText == 0)
   {
   alert("There are no tree species found. Please type in a valid species code");
   }
  else
  {
        findTask.execute(findParams,showResults2);
  }
    }

    function showResults2(results) {
        //This function works with an array of FindResult that the task returns
  map.graphics.clear();
  var markerSymbol = new esri.symbol.SimpleMarkerSymbol(esri.symbol.SimpleMarkerSymbol.STYLE_SQUARE, 12, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([0, 0, 0]), 1), new dojo.Color([0, 255, 255, .5]));
        var lineSymbol = new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_DASH, new dojo.Color([0, 255, 255]), 1);
        var polygonSymbol = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([0, 0, 0, 1]), 2), new dojo.Color([255, 255, 0, 0.5]));

  if(results.length == 0)
  {
   alert("There are no tree species found.");
  }
  else
  {
   //create array of attributes
   var items = dojo.map(results,function(result){
    var graphic = result.feature;
    switch (graphic.geometry.type) {
   case "point":
    graphic.setSymbol(markerSymbol);
    break;
   case "polyline":
    graphic.setSymbol(lineSymbol);
    break;
   case "polygon":
    graphic.setSymbol(polygonSymbol);
    break;
   }
   map.graphics.add(graphic);
   return result.feature.attributes;
   });
  }
 
        //Create data object to be used in store
        var data = {
          identifier: "AssetID",  //This field needs to have unique values
          //label: "Asset ID", //Name field for display. Not pertinent to a grid but may be used elsewhere.
          items: items
        };

        //Create data store and bind to grid.
        store = new dojo.data.ItemFileReadStore({ data:data });
        var grid = dijit.byId('grid');
        grid.setStore(store);

        //Zoom back to the initial map extent
        map.centerAndZoom(center, zoom);
      }
  
    //Zoom to tree when user clicks a row
    function onRowClickHandler(evt){
 
  var TreePoint = grid.getItem(evt.rowIndex).AssetID;
        var SelectedTreePoint;
 
        dojo.forEach(map.graphics.graphics,function(graphic){
          if((graphic.attributes) && graphic.attributes.AssetID === TreePoint){
            SelectedTreePoint = graphic;
            return;
          }
        });
        var TreePointExtent = SelectedTreePoint.geometry;
  map.centerAndZoom(TreePointExtent, .001);
      }
0 Kudos
5 Replies
KenBuja
MVP Esteemed Contributor
When you post code, could you please put them in the
 wrapper (the # in the formatting tools)? It make the code much easier to read.


The centerAndZoom method take either a zoom factor or level. I think that since you're providing it with a factor of 0.001, it's going to zoom in farther each time you click on a row. Try setting it to a zoom level instead (which appears do be what you're doing when you're zooming to the initial map extent).

Or you could test if the map is at a certain scale (getScale) or level (getLevel) and use centerAt instead of centerAndZoom.
0 Kudos
IanPeebles
Frequent Contributor
When you post code, could you please put them in the
 wrapper (the # in the formatting tools)? It make the code much easier to read.

The centerAndZoom method take either a zoom factor or level. I think that since you're providing it with a factor of 0.001, it's going to zoom in farther each time you click on a row. Try setting it to a zoom level instead (which appears do be what you're doing when you're zooming to the initial map extent).

Or you could test if the map is at a certain scale (getScale) or level (getLevel) and use centerAt instead of centerAndZoom.


Sorry about the code block post.  Here is where I am looking.  I cannot quite get what I am trying to achieve. . do you have any ideas how I can modify the block below?

        //Zoom back to the initial map extent
        map.centerAndZoom(center, zoom);
      }
  
    //Zoom to tree when user clicks a row
    function onRowClickHandler(evt){
 
  var TreePoint = grid.getItem(evt.rowIndex).AssetID;
        var SelectedTreePoint;
 
        dojo.forEach(map.graphics.graphics,function(graphic){
          if((graphic.attributes) && graphic.attributes.AssetID === TreePoint){
            SelectedTreePoint = graphic;
            return;
          }
        });
        var TreePointExtent = SelectedTreePoint.geometry;
  map.centerAndZoom(TreePointExtent, .001);
0 Kudos
VinayBansal
Frequent Contributor
As suggested by Kenbuga you can set the level instead of zoomfactor in centerAndZoom function.
Another way is that you can create a custom extent for the point feature
var factor = 100;
var extent;
 if (geom.type == "point") {
                    extent = new esri.geometry.Extent({ "xmin": geom.x - factor, "ymin": geom.y - factor, "xmax": geom.x + factor, "ymax": geom.y + factor, "spatialReference": { "wkid": 102100} });
                }
0 Kudos
IanPeebles
Frequent Contributor
As suggested by Kenbuga you can set the level instead of zoomfactor in centerAndZoom function.
Another way is that you can create a custom extent for the point feature
var factor = 100;
var extent;
 if (geom.type == "point") {
                    extent = new esri.geometry.Extent({ "xmin": geom.x - factor, "ymin": geom.y - factor, "xmax": geom.x + factor, "ymax": geom.y + factor, "spatialReference": { "wkid": 102100} });
                }


This worked really well!!! Thanks for the suggestion.  Initially I set the extent to the initial extent, but it was too taxing on the application.  Your suggestion worked like a charm.  Here is the updated code:

    //Zoom to Cemetery Space when User clicks a row
    function onRowClickHandler1(evt){
  var FindDeceasedName = grid1.getItem(evt.rowIndex).OBJECTID;
        var SelectedFindDeceasedName;
  
        dojo.forEach(map.graphics.graphics,function(graphic){
          if((graphic.attributes) && graphic.attributes.OBJECTID === FindDeceasedName){
            SelectedFindDeceasedName = graphic;
            return;
          }
        });
        var FindDeceasedNameExtent = SelectedFindDeceasedName.geometry;
  var factor = 100;
  var extent;
  
        extent = new esri.geometry.Extent({ "xmin": FindDeceasedNameExtent.x - factor, "ymin": FindDeceasedNameExtent.y - factor, "xmax": FindDeceasedNameExtent.x + factor, "ymax": FindDeceasedNameExtent.y + factor, "spatialReference": { "wkid": 2267} });
  map.setExtent(extent);
      }  
0 Kudos
VinayBansal
Frequent Contributor
Please mark the post as answered, so that others also benefit from this post.
0 Kudos