get Extent from Feature Layer after Definition Expression

4380
2
Jump to solution
04-08-2014 07:43 AM
Roy_A_Justo
New Contributor
Hello

I want to be able to type a state name in a

[HTML]<input type="text" id = "statename"></input>[/HTML]

and when I click a submit button, two things happen

1 - draw the state in the map ---> I already did this.

2 - zoom to the state extent. (I don't want to use the infoTemplate and then the Zoom to function. I want to be able to call this Zoom to function when I click the submit button)


the code:

var map, geocoder, extent, sr, currentExtent, censusdef, featureLayer, loadStates, renderer, selected_state;
require(["esri/map", "esri/layers/FeatureLayer", "esri/InfoTemplate",
    "esri/symbols/SimpleLineSymbol", "esri/symbols/SimpleFillSymbol",
    "esri/renderers/SimpleRenderer", "esri/tasks/query",
    "dojo/parser", "dojo/_base/Color",
    "dijit/layout/BorderContainer", "dijit/layout/ContentPane", "dojo/domReady!"], 
function(Map, FeatureLayer, InfoTemplate,
    SimpleLineSymbol, SimpleFillSymbol,
    SimpleRenderer,
    parser, Color, Query) { 
    map = new esri.Map("map", { 
        center: [-96.24, 38.1],
        zoom: 4,
        basemap: "hybrid" 
    });
    sr = new esri.SpatialReference({wkid:4326});
    currentExtent = new esri.geometry.Extent();


    var symbol = new SimpleFillSymbol().setColor(new Color([255,0,0,0.4]));
    var renderer = new SimpleRenderer(symbol);
    featureLayer = new esri.layers.FeatureLayer("http://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer/2", {
        //infoTemplate: new InfoTemplate(" ", "${state_name}"),
        mode: esri.layers.FeatureLayer.MODE_ONDEMAND,
        displayOnPan: true,
        styling: true
    });
    var b = document.getElementById('showstate');
    var selected_state = document.getElementById('statename').value;
    featureLayer.setDefinitionExpression("state_name = '"+selected_state+"'");
    featureLayer.setRenderer(renderer);

    b.addEventListener('click', function (){        
            var b = document.getElementById('showstate');
            var selected_state = document.getElementById('statename').value;
            //featureLayer.setDefinitionExpression("state_name = '"+selected_state+"'");
            featureLayer.setRenderer(renderer);
            map.addLayer(featureLayer);
        }, false);

});



is there anyway to access to the extent of the selected featureLayer? not the entire featurelayer, but only the result after the DefinitionExpression. Something like FeatureLayer.getExtent();

thanks!
0 Kudos
1 Solution

Accepted Solutions
TimWitt
Frequent Contributor
Maybe this example can help you?

Especially the following part:

        //Zoom to the parcel when the user clicks a row         function onRowClickHandler(evt) {           var clickedTaxLotId = evt.grid.getItem(evt.rowIndex).PARCELID;           var selectedTaxLot = arrayUtils.filter(map.graphics.graphics, function (graphic) {             return ((graphic.attributes) && graphic.attributes.PARCELID === clickedTaxLotId);           });           if ( selectedTaxLot.length ) {             map.setExtent(selectedTaxLot[0].geometry.getExtent(), true);           }         } 


Instead of clicking on a row, in your case clicking on a button would have this effect.

Hope this helps!

Tim

View solution in original post

0 Kudos
2 Replies
TimWitt
Frequent Contributor
Maybe this example can help you?

Especially the following part:

        //Zoom to the parcel when the user clicks a row         function onRowClickHandler(evt) {           var clickedTaxLotId = evt.grid.getItem(evt.rowIndex).PARCELID;           var selectedTaxLot = arrayUtils.filter(map.graphics.graphics, function (graphic) {             return ((graphic.attributes) && graphic.attributes.PARCELID === clickedTaxLotId);           });           if ( selectedTaxLot.length ) {             map.setExtent(selectedTaxLot[0].geometry.getExtent(), true);           }         } 


Instead of clicking on a row, in your case clicking on a button would have this effect.

Hope this helps!

Tim
0 Kudos
Roy_A_Justo
New Contributor
Thanks Tim.
This is a good approach.
I am having a hard time to understand why FeatureLayer object doesn't have a getExtent() Method. According to the API documentation, the purpose of this object was to substitute or enhance the Graphic Layer and server-client interaction. But it seems that if you want to play with zooming and extent you have to dig into graphic.geometry.

thanks a lot, it was very helpful.

Best
Roy
0 Kudos