Can I populate an InfoWindow with the selected feature's label?

4034
6
Jump to solution
05-14-2015 12:29 PM
BrettGreenfield__DNR_
Occasional Contributor II

I'm consuming a service that has symbology set up based on unique values of a particular field.  The way the service is set up, each symbology type has a label different than the actual unique value (as an example, one value is "Anthropogenic_Oyster_Reef" which is labeled as "Man-Made Oyster Reef").  I have a legend which displays the labels of the various symbology types, but when a user clicks a feature they're going to see the actual field value ("Anthropogenic_Oyster_Reef") instead of the label we've provided in the legend ("Man-Made Oyster Reef").  This can be confusing for our users.  Is there a way to retrieve the label of the feature when clicking it, or is my only option to edit the service so the field values match the desired labels?


I'm sure I could use some if statements to check for each possible field value and return the label name, but I feel like that would be extremely sloppy and there must be a better way.

0 Kudos
1 Solution

Accepted Solutions
SteveCole
Frequent Contributor

The labels are probably defined in the MXD that was used to publish the data so, more than likely, they were never actually part of the dataset's attributes.

Anyways, I suppose you could retrieve a copy of the layer's legend in JSON format using the REST API:

http://geodata.md.gov/imap/rest/services/Biota/MD_BenthicHabitat/MapServer/legend?f=json

JS code wise, something like this:

var url = "http://geodata.md.gov/imap/rest/services/Biota/MD_BenthicHabitat/MapServer/legend?f=json";
var requestHandle = esriRequest({
      url: url,
      content: {
          f: 'json'
      },
      handleAs: "json"
});

requestHandle.then(
    function (theJSON, io) {
                  //no problems encountered. Process content stored in theJSON..
    }, function (err) {
                  //Error encountered while retrieving the JSON version of the legend..
    }
};

The above would need AMD references for "dojo/_base/json", "esri/urlUtils", and "esri/request"

What you could do is step through the legend information and maybe build an array of code/labels. When your user clicks on a feature, use an infoWindow setContent function to look up the matching label and present whatever information you want to your users.

View solution in original post

0 Kudos
6 Replies
TimWitt2
MVP Alum

Hey Brett,

where is the label value for each feature stored?

Tim

0 Kudos
BrettGreenfield__DNR_
Occasional Contributor II

I'm not sure.  I tried looking through the object returned when you click a feature in the console, but I don't see the label stored anywhere in there.  It's obviously stored somewhere because the legend is able to retrieve that information.  Here's a link to the service I'm using if that will help: http://geodata.md.gov/imap/rest/services/Biota/MD_BenthicHabitat/MapServer/0

0 Kudos
TimWitt2
MVP Alum

I see, so it is the label value that was specified in the symbology before it got published. Let me do some research to see how that is accessible or somebody might already know it and post it here

TimWitt2
MVP Alum

Brett,

What if you add a field to your data named "LABEL" and then just do a select by attribute and calculate it with the label values. After you re-publish it, you can just use this label field in your pop-up.

This is just an idea if nobody is able to tell you how to access the Label that the legend widget accesses.

Tim

0 Kudos
SteveCole
Frequent Contributor

The labels are probably defined in the MXD that was used to publish the data so, more than likely, they were never actually part of the dataset's attributes.

Anyways, I suppose you could retrieve a copy of the layer's legend in JSON format using the REST API:

http://geodata.md.gov/imap/rest/services/Biota/MD_BenthicHabitat/MapServer/legend?f=json

JS code wise, something like this:

var url = "http://geodata.md.gov/imap/rest/services/Biota/MD_BenthicHabitat/MapServer/legend?f=json";
var requestHandle = esriRequest({
      url: url,
      content: {
          f: 'json'
      },
      handleAs: "json"
});

requestHandle.then(
    function (theJSON, io) {
                  //no problems encountered. Process content stored in theJSON..
    }, function (err) {
                  //Error encountered while retrieving the JSON version of the legend..
    }
};

The above would need AMD references for "dojo/_base/json", "esri/urlUtils", and "esri/request"

What you could do is step through the legend information and maybe build an array of code/labels. When your user clicks on a feature, use an infoWindow setContent function to look up the matching label and present whatever information you want to your users.

0 Kudos
BrettGreenfield__DNR_
Occasional Contributor II

Thanks Steve.  I figured that would probably end up being the case.  I'll probably just see about going and adding another field for the label.

0 Kudos