zoom to feature

2190
2
09-14-2020 11:40 AM
AlexRodriguez
New Contributor III

Javascript API v4.16
Building a page that opens up a map and zooms to the extent of the feature ID passed to it from the previous page.

So far:

1.  the feature ID is set in the "localStorage" of page 1

2.  in Page 2 I retrieve that value and attempt to zoom to that feature.

3.  I have a function that will do the zooming but I can't get it to work.

4.  Here is the function:

      

 function zoomToPassedFeature(strLocationCode){
   // Create a QUERY for a feature layer in the map
   const layQLayer = new FeatureLayer({
     url: strMapServiceURL + "/FeatureServer/3" // Yards layer
   }); 
   const query = new Query();
   query.where = "LocationCode = '" + strLocationCode + "'";
   layYards.queryExtent(query).then(function(results){
     view.goTo(results.extent);  // go to the extent of the results satisfying the query
   });   
 

   a.  if I put that function within the REQUIRE code block I get:  ReferenceError: Query is not defined

   b.  if I put that function outside the REQUIRE code block I get:  "zoomToFeatureExtent()" no defined.

How am I supposed to be doing this?

thanks!

Tags (1)
0 Kudos
2 Replies
KenBuja
MVP Esteemed Contributor

What does your require section look like? Are the function arguments in the same order as the modules?

0 Kudos
AlexRodriguez
New Contributor III

I finally figured it out.  The KEY being the first line in the section at the bottom:

 view.whenLayerView(layYards).then(function(layerView){...

The "whenLayerView" listener on the "view".

So now when I pass that FeatureID value and this layer is loaded it begins the process of querying and zooming.

Require Statement:

require([
  "esri/Map",
  "esri/views/MapView",
  "esri/layers/FeatureLayer",
  "esri/widgets/LayerList",
  "esri/widgets/Legend",
  "esri/widgets/Search",
  "esri/tasks/Locator",
  "esri/renderers/UniqueValueRenderer",
  "esri/widgets/Expand",
  "esri/tasks/IdentifyTask",
  "esri/tasks/support/IdentifyParameters"      
  ],
  function(
    Map
    MapView,
    FeatureLayer,
    LayerList,
    Legend,
    Search,
    Locator,
    UniqueValueRenderer,
    Expand,
    IdentifyTask
    IdentifyParameters
  ) {
...
// and the end of the JS file looks like:
...
  // Zoom to the feature ID passed from the AnL Site
  view.whenLayerView(layYards).then(function(layerView){
      var query = layYards.createQuery();
      query.where = "LocationCode = '" + strLocationCode + "'";
      layYards.queryExtent(query).then(function(results){
        view.goTo(results.extent);  // go to the extent of all the graphics in the layer view
        });
  });

}); // End of REQUIRE
0 Kudos