Newbie Question about toggling Feature Layers - JavaScript API

2996
5
11-15-2012 07:02 AM
DavidLetz
New Contributor
Hi, I am on my second week of learning the API, and have questions about controlling visibility of Feature layers. I have seen examples of doing this with basemap layers, but not feature layers. For example, say I have a map service that has 5 feature layers (parks, hospitals, fire stations, etc) for a certain Texas county. I need a way for the user to 1) toggle the visibility of each feature layer, and 2) the feature layer that is visible be functional as far as info windows/popups working with the symbols on the layer. I found this page http://blogs.uww.edu/giscenter/2011/08/11/toggle-feature-layers-with-arcgis-javascript-api/ but it is not making sense. Am I missing an example in the API samples that addresses this?

Thanks for your replies.
0 Kudos
5 Replies
MattO_Hara
New Contributor
Hi,

Toggling the visibility of feature layers is very similar to toggling the visibility of dynamic layers. There's an example here that shows that.

There's also APIs you can use to do this like gmaps-utility-gis. It provides a table of contents to toggle layer visibility as seen in this example.

On the maps I use I usually just put a checkbox for each layer with a class I know on them. Then I can attach an onclick event to those checkboxes like so:

        dojo.query(".layerSelectCheckbox").connect("onclick", function (ev) {
            /* check layer visibility */
        });
0 Kudos
DavidLetz
New Contributor
Thanks for the reply. Being the noob that I am, what I really need is a way to toggle the fields of a feature layer. For instance, I have a feature layer for the parks in my county. I want the user to be able to find parks with certain attributes, like pinic areas, pavillions, baseball fields, etc. So whne they click the checkbox for "pavillions" those parks that have paviilions will be shown on the map. Same for any other attribute. I was looking at this sample as a possible way: http://help.arcgis.com/en/webapi/javascript/arcgis/help/jssamples_start.htm.

Thanks again.
0 Kudos
CraigMcDade
Occasional Contributor III
Thanks for the reply. Being the noob that I am, what I really need is a way to toggle the fields of a feature layer. For instance, I have a feature layer for the parks in my county. I want the user to be able to find parks with certain attributes, like pinic areas, pavillions, baseball fields, etc. So whne they click the checkbox for "pavillions" those parks that have paviilions will be shown on the map. Same for any other attribute. I was looking at this sample as a possible way: http://help.arcgis.com/en/webapi/javascript/arcgis/help/jssamples_start.htm.

Thanks again.


Have you thought about using the ArcGIS for Local Goverment templates?
They have a Parks Finder Application that is very similar to what you have described. Fairly easy to set up.
0 Kudos
PaulBushore
Occasional Contributor
Hello,

Unless you break your data out in to ... parks with slides, parks with picnic tables etc. as individual layers you are probably going to have to go under the hood a bit and figure out about definition queries.  That way, when a person clicks a radio button or checkbox etc that value gets added to the definition query to add that type.

At first the definition query would be something simple like FID >1 then when a person clicks the picnic table checkbox it would append "AND picnicfield is not null" etc.  There is a sample that uses definition queries here: http://help.arcgis.com/en/webapi/javascript/arcgis/help/jssamples/fl_layer_definition.html

Of course, your attribute table of the parks will need to have the specific fields in there detailing what amenities are located at each park.

I haven't perused the ArcGIS Online Map that mcdade31 mentioned, but it is also worth a look.

Hope this helps!
0 Kudos
DavidLetz
New Contributor
In order for this toggle to work using feature layers that need explicit code to define the layer, outFields, popups or info windows, do I go ahed and define the layers as i would normally do so in my initialization of the map, such as below?

            flayer = new esri.layers.FeatureLayer("http://mymapserver1/ArcGIS/rest/services/Amenities/AllAmenities_test/MapServer/8", {
                mode: esri.layers.FeatureLayer.MODE_SNAPSHOT,
                infoTemplate: popupTemplate,
                outFields: ["NAME,ADDRESS,ZIPCODE,PHONE"]
            });
            map.addLayer(flayer);

and then add the code to call the map service with that layer and other feature layers?

Sorry, i am getting confused.
0 Kudos