Select to view content in your preferred language

How do I get individual values back from FeatureLayers on a click event?

5314
15
Jump to solution
04-28-2015 10:31 AM
ChrisSergent
Deactivated User

I want to be able to get individual values to populate this application: http://maps.decaturil.gov Currently when I click on a point on the map, its displays this:

StreetSignsInfoDisplay.png

How would I get just the value of the field OBJECTID for example?

0 Kudos
1 Solution

Accepted Solutions
TimWitt2
MVP Alum

You can use the onclick event of your feature layer to get any attribute you want:

yourlayer.on("click", function (evt){
  theAttributeYouWant = evt.graphic.attributes.OBJECTID;
});

Tim

View solution in original post

15 Replies
TimWitt2
MVP Alum

When you create the layer you can specify it in the outfield property

   layer = new FeatureLayer("//sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer/2", {
           
"id": "Washington",
           
"infoTemplate": popupTemplate,
           
"mode": FeatureLayer.MODE_SNAPSHOT,
           
"outFields": [OBJECTID],
           
"opacity": 0.8
        
});

ChrisSergent
Deactivated User

This is close but I don't want a template, I know how to do that. What I want is to get the individual values so I can take the values and write them to the form below. If I had a line of code  as an example, I could write the code for the other values. Any ideas? :

StreetSignsForm.png

0 Kudos
TimWitt2
MVP Alum

You can use the onclick event of your feature layer to get any attribute you want:

yourlayer.on("click", function (evt){
  theAttributeYouWant = evt.graphic.attributes.OBJECTID;
});

Tim

ChrisSergent
Deactivated User

I think I am doing it wrong. I wrote this:

app.citizenRequestLayer.on("click", function (evt) {

            myObject = evt.graphic.attributes.OBJECTID;

        });

and I get: Uncaught ReferenceError: myObject is not defined

0 Kudos
TimWitt2
MVP Alum

What happens if you leave app. off. just have it as citizenRequestLayer.on?

0 Kudos
ChrisSergent
Deactivated User

That won't work. app is an object for the purpose of unobtrusive JavaScript, so it's needed.

0 Kudos
TimWitt2
MVP Alum

You need to make sure that myObject is globally defined.

ChrisSergent
Deactivated User

It is.

0 Kudos
KenBuja
MVP Esteemed Contributor

Where are you defining the variable "myObject" and where do you get the error? Is it in the click callback or somewhere else?

0 Kudos