I'm trying to click on a country layer and use the country name as input for a GP tool. I've done this with XY inputs from map clicks, but I'm having a lot of trouble getting the attribute from a feature. Do I need a query task? Here is my code snippet. When I add content to the infoWindow, it displays "Country: ${NAME}" so it's not pulling that attribute from the feature layer correctly.
var country = {};
var countriesLayer = new FeatureLayer("http://<ourserver>/arcgis/rest/services/Countries_Outline/MapServer/1");
map.addLayer(countriesLayer);
var content = "<b>Country</b>: ${NAME}";
map.on("click", function(evt){
map.graphics.clear();
var graphic = new Graphic(evt.mapPoint);
map.graphics.add(graphic);
map.infoWindow.setFeatures([graphic]);
map.infoWindow.resize(200,200);
map.infoWindow.setContent(content);
map.infoWindow.show(evt.mapPoint)
map.infoWindow.setTitle("Forecast Reports");
});
var today = new Date();
var todayISO = today.toISOString().slice(0,10).replace(/-/g,"");
var link = domConstruct.create("a",{
"class": "action",
"id": "reportLink",
"innerHTML": "Forecast Report", //text that appears in the popup for the link
"href": "javascript: void(0);"
}, query(".actionList", map.infoWindow.domNode)[0]);
on(link, "click", function(){
domAttr.set(dom.byId("reportLink"), "innerHTML", "Generating Report...");
window.gp_chart = new Geoprocessor("http://<ourserver>/arcgis/rest/services/Generate_Forecast_Report/GPServer/CreateReport");
var country = window.map.infoWindow.getSelectedFeature();
var taskParams = {
"Country":country
};
//window.gp_chart.execute(taskParams,gpChartResultAvailable,gpChartFailure);
window.gp_chart.execute(taskParams);
Solved! Go to Solution.
Finally figured out a solution:
map.on("click", function uponClick(evt){
identifyParams.geometry = evt.mapPoint;
identifyParams.mapExtent = map.extent;
identifyTask.execute(identifyParams, function(result) {
for(var i = 0; i<result.length; i++){
country = result[i].feature.attributes["NAME"];
}
var infoTemplate = new InfoTemplate();
map.infoWindow.setTitle(country);
map.infoWindow.show(evt.mapPoint);
map.getInfoWindowAnchor(evt.mapPoint));
var link = domConstruct.create("a",{
"class": "action",
"id": "reportLink",
"innerHTML": "Generate Report",
"href": "javascript: void(0);"
}, query(".actionList", map.infoWindow.domNode)[0]);
map.infoWindow.setContent(link);
Lloyd,
You have the selected feature so you just have to get the attribute fro the selected feature:
var country = window.map.infoWindow.getSelectedFeature().attributes["country"];
OK. I keep getting this error: "Cannot read property 'Country' of undefined at HTMLAnchorElement.<anonymous>"
What is the line of code that is throwing this error?
This line. The attribute is called "NAME".
var country = window.map.infoWindow.getSelectedFeature().attributes["NAME"];
I also added this for the display and it threw the same error:
var country = countriesLayer.attributes["NAME"];
Lloyd,
So is
window.map.infoWindow.getSelectedFeature();
defined (i.e. not null).
Yes, it's defined "country."
Lloyd,
window.map.infoWindow.getSelectedFeature();
Should return a Graphic object...? Not a string.
OK, I'm trying to return a string. I need the name of the country I'm clicking on to be passed into the GP tool params.
Lloyd,
OK, but the
window.map.infoWindow.getSelectedFeature();
returns a Graphic then you get the attribute you want from that Graphic using Graphic.attributes.NAME or Graphic.attributes["NAME"] to get the actual string values of the NAME attribute.