What's the best way to zoom to a single feature after a QueryTask?

810
3
10-28-2021 08:18 AM
_____
by
New Member

On map load I want to zoom the map to a specific single feature, what's the best way to do this?

`executeForExtent` doesn't work because the single feature doesn't have an extent.

I think in the past I buffered the returned feature's geometry and zoomed to that created polygon feature. Is that still the best way to do this?

0 Kudos
3 Replies
JeffreyWilkerson
Occasional Contributor III

I think view.goTo(response.extent) will work with the response from your querytask.  Just a reminder that querytask (https://developers.arcgis.com/javascript/latest/api-reference/esri-tasks-QueryTask.html ) has been deprecated as of 4.21 and Esri is recommending the use of Query instead (https://developers.arcgis.com/javascript/latest/api-reference/esri-tasks-support-Query.html ).  This is Esri's sample page https://developers.arcgis.com/javascript/latest/sample-code/featurelayer-queryextent/ 

0 Kudos
_____
by
New Member

Thanks, that does seem to work, but I don't understand how it's setting the scale level. It's too far out for what I'm wanting, but it does locate the feature.

I think I'll just keep doing it how I always have since I can easily get the scale level I want using expand:

 

servicePoint_QT.execute(meterNumber_Q).then(zoomToServicePoint);

function zoomToServicePoint(results) {
    var geometry = results.features[0].geometry;
    var buffer = geometryEngine.buffer(geometry, 10, 'feet');

    view.extent = buffer.extent.expand(3);
}

 

0 Kudos
JohnGrayson
Esri Regular Contributor

Is using goTo() as Jeffery mentions, and working with point geometries and you need a specific zoom scale, you might want to try using some of the other 'goTo()' parameters, maybe something like this:

servicePoint_QT.execute(meterNumber_Q).then(zoomToServicePoint);

function zoomToServicePoint(results) {
    var geometry = results.features[0].geometry;
    view.goTo({target:geometry, scale: 12500});
}


https://developers.arcgis.com/javascript/latest/api-reference/esri-views-MapView.html#goTo

https://developers.arcgis.com/javascript/latest/api-reference/esri-views-MapView.html#GoToOptions2D