view.goTo() for Polygons

556
3
Jump to solution
10-14-2022 01:30 PM
DaltonR121
New Contributor II

What I'm trying to achieve:
I'm attempting to load only polygons associated with a specific ID.  If only one record exists for that ID, I would like for the map to automatically center that polygon in the view.  If there are multiple records with the specified ID, I would like for the view to be the center of all polygons. 

I believe I have a disconnect on how to use the view.goTo() method and would love if someone could help me sort it out!

I'll have a code snippet at the bottom but here is my thought process in case this helps analyze what I've got going on any.

- Grab layer URL
- Create new Feature Layer from URL, using Definition Expression to only return associated records
- Add layer to map
- Create an array to hold geometries in case there are multiple records (multiple polygons) associated with record
- Query the layer to return the geometry
- Loop over results and fill array created with geometry from each result.
- Pass the array of geometries to the view.goTo() method.

I was previously keying into layer.geometry.rings and that didn't work so I thought maybe it wants the whole geometry object.  So instead of "featureGeometry" being an array of arrays, it's currently an array of objects.

I have been referencing this documentation but can't quite seem to figure out where I'm going wrong.  I am not getting any errors whatsoever.   All of my console logs are printing as I would expect them to.  

Side question: Is there a way I could just return the geometries in the Feature Layer definition instead of needing to make an extra query?  

Any help getting the view to go to the center of polygon(s) is MUCH appreciated!

const programURL = "https://services3.arcgis.com/YCgdaDV3RW2dDPyk/arcgis/rest/services/program/FeatureServer/0"; 

const programLayer = new FeatureLayer({
      url: programURL,
      outfields: ["*"],
      definitionExpression: `recordID = ${recordId}`,
    });

    // Add layer to map
    map.add(programLayer);

    let featureGeometry = [];

    if (recordId) {
      const query = {
        where: `recordId = ${recordId}`,
        returnGeometry: true,
        outFields: ["*"],
      };

      programLayer.queryFeatures(query).then((results) => {
        if (results.features.length > 0) {
          results.features.forEach((layer) => {
            console.log(layer.geometry);
            featureGeometry = [...featureGeometry, layer.geometry];
          });
        }
      });
    }

    console.log(featureGeometry);

    view.goTo({
      target: featureGeometry
    }).catch(function (error) {
      if (error.name != "AbortError") {
        console.error(error);
      }
    });

 

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

If you just want to get the extent of the queried features, use queryExtent instead of queryFeatures.

layer.queryExtent(query).then(function(results){
  view.goTo(results.extent);  // go to the extent of the results satisfying the query
});

View solution in original post

0 Kudos
3 Replies
GeoJason
New Contributor III

I'm not at my PC at the moment but maybe you could return the extent of the layer? That will make sure both (or more) features are in the map view. 

extent:

Add this under the target with the feature geometry in it.

Sorry I'm on my phone so formatting is not great. 

0 Kudos
KenBuja
MVP Esteemed Contributor

If you just want to get the extent of the queried features, use queryExtent instead of queryFeatures.

layer.queryExtent(query).then(function(results){
  view.goTo(results.extent);  // go to the extent of the results satisfying the query
});
0 Kudos
DaltonR121
New Contributor II

Wow.  That was incredibly simple.  Thank you so much for your help.

0 Kudos