Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am using the ArcGIS API for JavaScript.

My layer of Point features is exposed through a REST service. I want to retrieve all of these features when the application starts up, then show different subsets of features at different times, depending on user input (such as text search) and client-side calculations (such as clustering calculations). If I just create a FeatureLayer normally, it won't actually retrieve any features before it's added to a map -- its graphics property is just [] when I look at it in Firebug. I tried calling FeatureLayer.refresh, but it just complains that map is undefined.

How do I retrieve the features without having to attach them to a map?

share|improve this question
add comment

3 Answers 3

ArcGIS Javascript doesn't have any classes to handle something like this.

How much data are we talking about? For a smaller set, you could query all the data back with 1=1 query and add it to a list or array or something to a FeatureSet (JQuery might have something good for you to hold these). Then based on user's input, you loop over the FeatureSet/list and match the specific attribute value. You can then add the returned subset to a graphics layer and display it on the map. This might require some good code writing because there are no pre-written classes for this. JQuery will have some good classes for parsing through objects likes this, if you are familiar with that.

For a larger dataset, you may have to think of creating a SQL table or a JSON file or something that retrieves all the data from ArcGIS server on the application load (might be hard to trigger) or periodically, then you just retrieve what you need from that table based on some queries.

share|improve this answer
add comment

why not just set visible to "false" in the FeatureLayer constructor, add it to the map in snapshot mode to ensure all features are downloaded and then set your definition expression later before toggling visibility on?

featureLayer = new FeatureLayer("yoururl",{
  mode: FeatureLayer.MODE_SNAPSHOT,
  ...       
  visible:false
});
map.addLayer(featureLayer);

...

function userChoseSomething() {
  featureLayer.setDefinitionExpression(SQL = 'fun');
  featureLayer.visible = true;
}
share|improve this answer
    
I tried this, but with visible: false, the data doesn't actually get downloaded. –  alcedine Aug 22 '13 at 12:49
    
i noticed that myself after posting. i guess that means its not a very good 'answer' to your original question, but given the API requirement that you have to load featureLayers in the map before you can do much with them, i still think its a good way to get the kind of control you seem to want. –  John Gravois Aug 23 '13 at 21:49
add comment

I ended up deciding to run a small init script that adds the features to the map as a FeatureLayer, and (as soon as the update-end event fires on the layer, meaning that the features are in) copies off the graphics and the renderer, then cleans up after itself:

var myMap = new Map("mapDiv", {
    center: centerPoint,
    zoom: initialZoom,
    basemap: "topo" });

    ...

var dataLayer = new FeatureLayer(dataRestURI, {mode: FeatureLayer.MODE_SNAPSHOT, outFields: ["*"]});
on(dataLayer, "update-end", function(){ initData(dataLayer); });
myMap.addLayer(dataLayer);

    ...

function initData(layer) {
    var graphics = layer.graphics;
    var renderer = layer.renderer;
    esriMap.removeLayer(layer);
    delete layer;

    // Do something with this
};

Thanks to Mr. Gravois and Garima V for taking the time to respond. Like I said, setting visible: false made the layer simply not retrieve any features. A query-based solution might work, but it appears that a headless query requires some additional fiddling, so I explored the route that led me to the solution above.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.