How to view.goTo feature result and display full extent of feature?

3526
8
Jump to solution
08-14-2017 11:56 AM
KirstenFrost_Andersen
New Contributor III

I have some JavaScript that queries for a parcel in a feature layer, successfully, and then zooms to the parcel using view.goTo(). It works great, except it zooms a little TOO close into the parcel. I'd like to have it fully display the parcel on the map, not go too far in.  Any ideas?

Using ESRI's JavaScript 4.4 API

view.then(function() {
    var url = urlUtils.urlToObject(document.URL);
    //make sure we have parameters
    if (url.query) {
     var parcelNumber = url.query.PID;
     return parcelLayer.then(function() {
      var query = parcelLayer.createQuery();
      query.where = "PID_NUM = '" + parcelNumber + "'";
      query.outSpatialReference = view.spatialReference;
      return parcelLayer.queryFeatures(query);
     });
    }
   })
   .then(zoomTo)
   .then(highlight);
  
   function zoomTo(response) {
    var parcel = response.features[0];
    //var parcelExtent = response.features[0].geometry.getExtent().expand(0.5);
    view.goTo(parcel);
    //map.setExtent(parcelExtent);
    return (parcel);
   }

Here is my zoomed map with the code above -- as you can see, it's not showing the full extent of the polygon I need to have zoomed into: 

Here is a more preferable view one click zoomed out (though I'd rather have the polygon fill the space, but be completely visible in the map): 

Thanks for any ideas.

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Esteemed Contributor

Kirsten,

   Have you tried:

var parcelExtent = response.features[0].geometry.extent.clone().expand(0.5);
view.goTo(parcelExtent);

Updated: 8/15/17 8:13 AM

View solution in original post

8 Replies
KenBuja
MVP Honored Contributor

In the 3.x version, the setExtent method had the fit option to have the input extent shown completely on the map. It looks like this was lost in the 4.x version of goTo and extent. I don't remember if was a very early version of JS API or the old Flex API where this wasn't an option, but the workaround for it was to use Extent.contains to see if the feature's extent was contained by the map extent. If not, zoom out one level.

0 Kudos
RobertScheitlin__GISP
MVP Esteemed Contributor

Kirsten,

   Have you tried:

var parcelExtent = response.features[0].geometry.extent.clone().expand(0.5);
view.goTo(parcelExtent);

Updated: 8/15/17 8:13 AM

View solution in original post

ThomasSolow
Regular Contributor

I would second this, although I don't think the .getExtent function made it into 4.XX.

Also, .expand modifies the extent in place so it's probably best to clone it first.

RobertScheitlin__GISP
MVP Esteemed Contributor

Thanks Thomas. I updated my post.

0 Kudos
KirstenFrost_Andersen
New Contributor III

Thanks guys, just got a chance to dig back in today.  I broke the zoom:

function zoomTo(response) {
    //var parcel = response.features[0];
    var parcelExtent = response.features[0].geometry.getExtent().expand(0.5);
    view.goTo(parcelExtent);
    return (parcel);
   }
0 Kudos
RobertScheitlin__GISP
MVP Esteemed Contributor

Kirsten,

   Make sure you try my updated code not what I posted originally.

KirstenFrost_Andersen
New Contributor III

That must be what I did wrong -- missed your update.  It looks like it works, though it zooms way too far in now, so I'll adjust.  Thank you!

0 Kudos
ThomasSolow
Regular Contributor

It looks like .5 is 50%, which will shrink it.  That's a little confusing because the method is called "expand," but I'd give 1.5 a try to grow the extent by 50%.