I'm using the follow snippet of code to zoom to the extent of graphics in a featurelayer. It works great when the map starts at the full extent. If I remove some graphics and add new graphics that are in an area that is not currently visible on the map, the queryExtent returns a null extent. However, if I add code to zoom to the full extent of the map first, then the queryExtent returns the extent. Anyone know how I can get this to work without zooming home each time I want to zoom to the extent of the layer?
function zoomToLayer(layer) {
/*first zoom to home to get around queryExtent bug.
Getting the queryExtent for a layerview does not work if the
extent of the features is not visible in the map */
/*
view.goTo({
center: [-94.25,45.98],
zoom: 7
});*/
view.whenLayerView(layer).then((layerView) => {
var handle = layerView.watch("updating", function(val) {
// wait for the layer view to finish updating
if (!val) {
layerView.queryExtent().then(function(response) {
console.log(response);
//remove the layer updating watch handler
handle.remove();
// go to the extent of all the graphics in the layer view
view.goTo(response.extent);
});
}
});
});
}