PopupTemplate 简介

尝试一下在线预览

弹出窗口为用户提供了一种从图层图形访问数据属性的简便方法。它们允许用户单击地图中的要素以查看特定于所选要素的内容,从而增强了自定义应用程序的交互性。

每个视图都有一个与之关联的弹出窗口。在大多数情况下,弹出窗口的内容是使用特定于您的数据的模板设置的。该模板是使用 PopupTemplate 定义的。PopupTemplate 可以在单个图形或图层上设置。当用户选择图层内的要素时,视图的弹出窗口将根据分配给该图层的 PopupTemplate 设置其内容。

在最基本的情况下,您不需要修改视图弹出窗口的属性。相反,大多数弹出窗口自定义可以使用 PopupTemplate 进行处理。

此示例将引导您完成如何在 FeatureLayer 中使用 PopupTemplate。此处提到的原则同样可应用于其他图层类型,例如 ImageryLayer

在完成以下步骤之前,您应该熟悉视图地图FeatureLayer。如有必要,请先完成以下教程:

除了下面提供的步骤之外,您还可以直接在内容中通过 text 类型元素直接设置 PopupTemplate.content。有关详细信息和示例,请参阅其 参考手册和演示如何显示多个 popupTemplate 元素的示例。

1. 创建 FeatureLayer 并将其添加到地图

使用此服务创建描述纽约邮政编码的各种统计信息的 FeatureLayer。接下来,将其添加到基本 Map 中,然后将 Map 实例设置为 View。您的 JavaScript 应类似于下面显示的内容:

                        
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
require(["geoscene/Map", "geoscene/views/MapView", "geoscene/layers/FeatureLayer"], (
  Map,
  MapView,
  FeatureLayer
) => {
  // Create a FeatureLayer
  const featureLayer = new FeatureLayer({
    url: "https://services.arcgis.com/P3ePLMYs2RVChkJx/arcgis/rest/services/ACS_Marital_Status_Boundaries/FeatureServer/2"
  });

  // Create the Map and add the featureLayer defined above
  const map = new Map({
    basemap: "gray-vector",
    layers: [featureLayer]
  });

  // Create the MapView
  const view = new MapView({
    container: "viewDiv",
    map: map,
    center: [-73.95, 40.702],
    zoom: 10
  });
});

2. 创建 PopupTemplate 对象并添加标题

PopupTemplate 允许您在弹出窗口的标题和内容属性中显示要素属性或特定于所选要素的数据(如区域名称、人口、州代码等)。属性在以下语法中引用:{AttributeName}

PopupTemplate 具有一个标题属性,该属性将显示在弹出窗口的顶部。由于此图层的渲染器按邮政编码描绘了结婚率,因此我们将添加与所选地理位置的邮政编码相关的标题。

    
1
2
3
4
const template = {
  // NAME and COUNTY are fields in the service containing the Census Tract (NAME) and county of the feature
  title: "{NAME} in {COUNTY}",
};

由于 PopupTemplate 可能是自动转换的,因此您不需要 geoscene/PopupTemplate 模块。只需创建一个包含与 PopupTemplate 文档中指定的属性相同的属性的简单对象,并将其设置在图层的 popupTemplate 属性上即可。

3. 将内容添加到 PopupTemplate

PopupTemplate 的内容属性提供了一个模板,用于定义每个选定要素的弹出窗口内容。在此应用程序中,我们希望添加一个简单的表,其中列出了与所选功能相关的婚姻统计信息。内容从 fieldInfos 填充的一个对象数组。

将以下内容添加到模板的内容中:

                           
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
const template = {
  // autocasts as new PopupTemplate()
  title: "{NAME} in {COUNTY}",
  content: [
    {
      type: "fields",
      fieldInfos: [
        {
          fieldName: "B12001_calc_pctMarriedE",
          label: "Married %"
        },
        {
          fieldName: "B12001_calc_numMarriedE",
          label: "People Married"
        },
        {
          fieldName: "B12001_calc_numNeverE",
          label: "People that Never Married"
        },
        {
          fieldName: "B12001_calc_numDivorcedE",
          label: "People Divorced"
        }
      ]
    }
  ]
};

可以添加纯文本以外的其他内容类型。您还可以将图像,图表,表格,附件以及上述内容的任意组合添加到弹出窗口中。有关此示例,请参阅多个弹出窗口元素示例

弹出窗口应如下所示:

new-popuptemplate-no-formatting

这很好,但格式化值将有助于内容看起来更清晰。

4. 使用 fieldInfos 以格式化字段值

PopupTemplatefieldInfos 属性允许设置数字和日期字段的格式。在这种情况下,我们希望对大于 1000 的值使用逗号分隔符,并在提供任何十进制值的情况下四舍五入到最接近的整数。

有关如何实现这一点,请参见下面的代码:

                                       
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
const template = {
  // autocasts as new PopupTemplate()
  title: "{NAME} in {COUNTY}",
  content: [
    {
      type: "fields",
      fieldInfos: [
        {
          fieldName: "B12001_calc_pctMarriedE", // The field whose values you want to format
          label: "Married %"
        },
        {
          fieldName: "B12001_calc_numMarriedE", // The field whose values you want to format
          label: "People Married"
          format: {
            digitSeparator: true, // Uses a comma separator in numbers >999
            places: 0 // Sets the number of decimal places to 0 and rounds up
          }
        },
        {
          fieldName: "B12001_calc_numNeverE", // The field whose values you want to format
          label: "People that Never Married"
          format: {
            digitSeparator: true, // Uses a comma separator in numbers >999
            places: 0 // Sets the number of decimal places to 0 and rounds up
          }
        },
        {
          fieldName: "B12001_calc_numDivorcedE", // The field whose values you want to format
          label: "People Divorced"
          format: {
            digitSeparator: true, // Uses a comma separator in numbers >999
            places: 0 // Sets the number of decimal places to 0 and rounds up
          }
        }
      ]
    }
  ]
};

5. 在图层上设置 PopupTemplate

图层的 popupTemplate 属性上设置模板。这可以在 FeatureLayer 的构造函数中处理,也可以在将图层实例添加到地图之前直接在图层实例上进行处理。

 
1
featureLayer.popupTemplate = template;

上面的代码段指示弹出窗口应显示在视图上。此外,在此弹出实例上设置了已定义的模板。

其余步骤将引导您完成在 PopupTemplate 的构造函数中设置模板内容的过程。这通常在图层上设置模板之前进行处理。

现在,弹出内容应按以下方式进行格式化:

popuptemplate-with-formatting

单击上面的 尝试一下 按钮,查看此完整示例及其代码。

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.