1

I'm trying to return a string when I click on a country in a map. The string is the country name, which will then be passed to a geoprocessing tool. I've definied "country" as a global variable. I'm trying to define it inside the click function and it's not working. console.log() doesn't print anything.

Here is the code snippet:

var country = {};
map.on("click", function(evt) {
  var selectQuery = new Query();    
  selectQuery.returnGeometry = true; 
  selectQuery.geometry = geometry;
  featureLayer.selectFeatures(selectQuery, FeatureLayer.SELECTION_NEW, function(results) {
    country = results[0].attributes["NAME"];  
    console.log(country);           
  });
  map.infoWindow.setContent(content);
});
10
  • Does var country need to be an object? What is returned by the selection?
    – Barbarossa
    Commented Mar 7, 2017 at 20:10
  • It needs to be a global variable so I can pass it from one function to another. results[0].attributes["NAME"] is supposed to return the name of the country that's been clicked, since FeatureLayer.SELECTION_NEW returns an array of graphics, not strings. I don't think it's returning anything though, because nothing is being logged to the console when I click a country. Commented Mar 7, 2017 at 20:13
  • Right, so why not just use var country;
    – Barbarossa
    Commented Mar 7, 2017 at 20:14
  • I've tried that. Won't it stay an empty variable once it's out of the scope of the functions? Commented Mar 7, 2017 at 20:16
  • It should not. All scripts and functions should be able to access and modify it.
    – Barbarossa
    Commented Mar 7, 2017 at 20:17

2 Answers 2

2

This is the solution. I needed to set the content after defining the link.

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);
1

i think you should listen to the click event of your featureLayer instead of the map, so you can access the attributes of the feature. something like :

var country = ''; //"{}" means object but you want a string
On(featureLayer, 'click', function (e) {
  country = e.graphic.attributes.name;
});

Add "dojo/on" as a parameter of require, ie :

require([..., "dojo/on", ...], function (..., On, ...)

here is a code i posted https://stackoverflow.com/a/42419932/1851595

11
  • OK. I tried this and got the same results. Country is "undefined" if it's set as a string, and "object" if it's set as an object. var country = ''; on(countriesLayer, 'click', function (e) { var content = "<b>Location: </b> ${NAME}"; var infoTemplate = new InfoTemplate("Forecast Reports", content); country = e.graphic.attributes.name; console.log(country); map.infoWindow.setContent(content); }); Commented Mar 8, 2017 at 15:17
  • I need country to be global, so I can retrieve it from a click event. A link is created in the info window to a GP tool. Country needs to be passed into that when the link is clicked. I've tried a totally different approach with making the layer dynamic and using an identify task, and I'm able to return country, but I can't get the GP tool link into the popup. Commented Mar 8, 2017 at 15:19
  • Sorry but what you want is not clear. You first asked to get the country name in a string, when you click on it. Now, you want the country name to be in link which is in an info window ? And you want a link to the GP tool in the info window ?
    – LMokrane
    Commented Mar 8, 2017 at 15:55
  • The name is showing up in the info window, that's never been a problem. I just need the name passed out as a string to the GP tool. I have a link to the GP tool in the popup, but it kept erroring out when I was trying to pass in the country string from a global variable. The initial click which pulls the country name and sets up the infotemplate and the GP tool itself are two different functions, which is why I needed it to be global. Commented Mar 8, 2017 at 16:08
  • But I've scrapped that approach and changed to a dynamic layer and used an identify task. This passes the country name string just fine. I've also tried to nest the GP tool link function into the initial click function, but the link is not showing up in the infowindow. That's where I'm stuck. Commented Mar 8, 2017 at 16:14

Your Answer

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

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