1

I have been struggling for some time with selecting points from a featureLayer which is stored on an ArcGIS webserver. Each of these point graphics contains information such as an ObjectID, SiteName, City, County, etc. I have created an infoTemplate for the points and all of this data is shown when each graphic point is clicked. However, when trying to access the values of the graphic, such as the value of ObjectID, I cannot seem to find anything other than an empty object.

Here is what I have tried:

 <script src="http://js.arcgis.com/3.8/"></script>
 <script>
 dojo.require("esri.map");
 dojo.require("esri.layers.FeatureLayer");
 var map;
 var featureLayer;
 var infoTemplate;
 function init(){
 map = new esri.Map("mapDiv", {
 basemap: "topo",
 center: [-87.7, 41.9],
 zoom: 11
 });
 featureLayer = new esri.layers.FeatureLayer("http://147.126.65.155/gis/rest/services/caws/sample_sites/FeatureServer/0", {
 mode: esri.layers.FeatureLayer.MODE_SNAPSHOT,
 outFields: ["*"],
 opacity:1
 });
 infoTemplate = new esri.InfoTemplate("ObjectID : ${objectid}", 
 "SiteID : ${siteid}<br/>"+
 "Site Name : ${sitename}<br/>"+
 "SourceID : ${sourceid}<br/>"+
 "Source's Site Name : ${sourcesiteid}<br/>"+
 "StateID : ${state}<br/>"+
 "CountyID : ${county}<br/>"+
 "CityID : ${city}<br/>"+
 "ZIP Code : ${zipcode}<br/>"+
 "NeighborhoodID : ${neighborhood}<br/>"+
 "Latitude : ${latitude}<br/>"+
 "Longitude : ${longitude}");
 featureLayer.setInfoTemplate(infoTemplate);
 map.addLayers([featureLayer]);
 dojo.connect(map, "onLayersAddResult", initEditing);
 }
 function initEditing(results) {
 console.log("We're within initEditing!");
 var currentLayer = null;
 var layers = dojo.map(results, function(result) {
 console.log(result.layer);
 return result.layer;
 })
 dojo.forEach(layers, function(layer) {
 dojo.connect(layer, "onClick", function(evt) {
 console.log("I just clicked a layer!");
//AT THIS LINE I WANT TO SEND INFORMATION ABOUT THE SELECTED LAYER TO MY SERVER
//SIDE DJANGO APPLICATION
 console.log(layer.getSelectedFeatures());
 });
 });
 }
 dojo.ready(init);
 </script>

The console output is simply an empty array []

I know that information exists about the feature since it is stored and displayed in the infoTemplate.

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Jan 28, 2014 at 19:33

1 Answer 1

2

Inside your onClick event handler, you can access the clicked graphic as a property of the event object. Change:

console.log(layer.getSelectedFeatures());

To:

console.log(evt.graphic);

The reason that getSelectedFeatures() doesn't work is that, by default, clicking a feature doesn't select it (I realize this is confusing as it seems like a feature is selected since it's highlighted and the popup is shown with feature attribute info). The getSelectedFeatures() method will return features when a selection has been created with selectFeatures().

Here's a simple page that logs the clicked feature to the console: http://jsbin.com/ehELesI/1/edit (modified version of the on demand feature layer sample)

answered Jan 29, 2014 at 1:36

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.