geodev-hackerlabs

Style a Layer Popup

In this lab you will use code to style a popup.

  1. Click create_starter_map/index.html and copy the contents to a new jsbin.com.

  2. In JSBin > HTML, update the require statement and function definition:

  require([
    "esri/Map",
    "esri/views/MapView",
    /*** ADD ***/
    "esri/layers/FeatureLayer",
    "esri/PopupTemplate",
    "dojo/domReady!"
  ], function(Map, MapView, FeatureLayer, PopupTemplate) {
  1. Now add create a new PopupTemple with the popup template style desired:
    ...

    var view = new MapView({
      container: "viewDiv",
      map: map,
      center: [-122.68, 45.52],
      zoom: 10
    });

    /*** ADD ***/

    var popupTemplate = new PopupTemplate({
      title: "Neighborhoods",
      // Fields
      content: [{
        type: "fields",
        fieldInfos: [
          { fieldName: "TOTPOP_CY", label: "Total Population", visible: true, format: { places: 0 } },
          { fieldName: "AVGHINC_CY", label: "Average Income", visible: true, format: { places: 0 } },
          { fieldName: "MEDAGE_CY", label: "Median Age", visible: true, format: { places: 0 } },
          { fieldName: "AREA", visible: true, format: { places: 2 } }
        ]
      },
      {
        type: "media",
        mediaInfos: [{
          title: "Demographics",
          type: "pie-chart",
          value: { 
            fields: [ 
              "TOTPOP_CY", 
              "AVGHINC_CY", 
            ]
          }
        }]
      }] 
    });
  1. Now add the template to the feature layer and add the featurelayer to the map.
    var featureLayer = new FeatureLayer({
      url: "http://services.arcgis.com/uCXeTVveQzP4IIcx/arcgis/rest/services/PDX_Neighborhoods_Enriched/FeatureServer/0",
      outFields: ["*"],
      popupTemplate: popupTemplate
    });

    map.add(featureLayer);
  1. Confirm that the JSBin Output panel shows styled popups when you click on the neighborhoods.

Your app should look something like this:

Bonus