Search code examples
javascriptarcgisarcgis-js-api

ArcGIS JavaScipt API; check if old Extent currently visible is in new Extent


I am using the ArcGIS JavaScript API for showing an ArcGIS map in my application. I also have filters on my page that are used to create a query definition to ArcGIS server, to filter down on the objects shown. When these filters are applied, the map zooms to the Extent applicable for the current selection agreeing to the search filters I entered.

Users also sometimes already zoom in to some part of the map and then hit the filter options. If more objects, outside of the current users view, also agree to the filters, the map zooms out to the view where all objects agreeing to the filters are visible again.

Ideally I would like to check, before appliying the new extent for the search filers, if the current etent is already inside (more zoomed in) in this new extent. If so, I would not like the map to zoom to the greater Extent.

Currently I use below code, to gather all objects agreeing to the filters/query. I commented (v1.5.0 comment) the 'setExtent' part for now, but ideally I would like a check there whether the current extent is already inside this new Extent. If so, do not zoom / use setExtent, if not so, do zoom / use setExtent:

var qt = new esri.tasks.QueryTask(identifyTaskLayerURL);        

qt.execute(q,lang.hitch(this, function(response) {

    if (response && response.features){

        for (var k = 0 ; k < response.features.length ; k++){

            // extract needed content from QueryTask result
            var geometry = response.features[k].geometry;

            if (geometry){

                centerLocation = this._getCenterCoordinates(geometry);                      
                centerX = centerLocation.x.toFixed(5).toString();
                centerY = centerLocation.y.toFixed(5).toString();

                multiPoint.addPoint(new esri.geometry.Point(centerX,centerY));

            } else{
                console.log(this._logNode + "query gave empty result");
            }
        }
        if (this._referenceMxObjectsArr.length == 1){
            this._gisMap.centerAndZoom(centerLocation,Number(this._singleObjectZoom) - 1);
        } else {
            // set extent
            this._extent = multiPoint.getExtent();
            this._extent.setSpatialReference(this._gisMap.spatialReference);
            // v1.5.0 disabled next line
            //this._gisMap.setExtent(this._extent);
        }
    }                   

}));

Solution

  • You could use the contains method of the extent geometry to check that, something like this should work,

    this._extent = multiPoint.getExtent();
    this._extent.setSpatialReference(this._gisMap.spatialReference);
    if (!mapExtent.contains(this._extent) {
      this._gisMap.setExtent(this._extent);
    }
    

    To get the current extent depends the version of the API you are using.

    With API 4.x you need to use the map view, mapView.extent.

    With API 3.x you need to use the map, map.extent