图形介绍

尝试一下在线预览

图形是 GeoScene API for JavaScript 的重要组成部分。您可以使用它们来显示具有简单符号的地图上的位置,或者用于显示查询或其他方法的结果。使用图形越舒适,处理几何图层弹出窗口就越流畅。

本教程将向您展示如何向地图添加简单图形、使用自定义符号设置其样式以及向其添加属性。

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

1. 创建简单地图

首先创建一个简单的地图,并在视图中引用它。您的 JavaScript 可能类似于下面的代码:

              
1
2
3
4
5
6
7
8
9
10
11
12
13
14
require(["geoscene/Map", "geoscene/views/MapView"], (Map, MapView) => {
  // Create the Map
  const map = new Map({
    basemap: "hybrid"
  });

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

2. 了解图形的属性

每个图形都有四个主要属性:

属性类型说明
geometryGeometry在实际坐标中定义图形的几何或位置。
symbolSymbol定义图形在视图中的渲染方式。
attributesObject包含描述图形所表示的要素的数据或属性。
popupTemplatePopupTemplate允许用户在弹出窗口中访问图形的属性。

其余步骤将引导您完成如何使用其中每个属性。

3. 创建线几何

我们希望在地图中添加一个线图形,以显示 Keystone 管道建议位置的通用版本。

需要线模块并创建表示此位置的新线几何。您将使用 path 属性向线添加折点。

           
1
2
3
4
5
6
7
8
9
10
11
require(["geoscene/Map", "geoscene/views/MapView"], (Map, MapView) => {
  //
  // This is where you created a Map and MapView in the previous steps
  //

  // Create a line geometry with the coordinates of the line
  const polyline = {
    type: "polyline", // autocasts as new Polyline()
    paths: [[-111.3, 52.68], [-98, 49.5], [-93.94, 29.89]]
  };
});

4. 创建线符号

现在,您已经有了管线位置的几何,您需要一个符号来渲染视图中的线。使用 SimpleLineSymbol 为管道创建线符号。

             
1
2
3
4
5
6
7
8
9
10
11
12
13
require(["geoscene/Map", "geoscene/views/MapView"], (Map, MapView) => {
  //
  // This is where you created a Map, MapView,
  // and geometry in the previous steps
  //

  // Create a simple line symbol for rendering the line in the view
  const lineSymbol = {
    type: "simple-line", // autocasts as new SimpleLineSymbol()
    color: [226, 119, 40], // RGB color values as an array
    width: 4
  };
});

5. 创建要存储在图形中的属性

属性可以提供有关图形的关键信息,这些信息可以增强应用程序的实用性。在此步骤中,我们将向线图形添加三个基本属性:nameownerlength

             
1
2
3
4
5
6
7
8
9
10
11
12
13
require(["geoscene/Map", "geoscene/views/MapView"], (Map, MapView) => {
  //
  // This is where you created a Map, MapView,
  // geometry, and symbol in the previous steps
  //

  // Create a simple object containing useful information relating to the feature
  const lineAtt = {
    Name: "Keystone Pipeline", // The name of the pipeline
    Owner: "TransCanada", // The owner of the pipeline
    Length: "3,456 km" // The length of the pipeline
  };
});

6. 创建图形并指定其几何、符号和属性

需要图形模块,创建图形,并将其添加到 MapView 上的图形属性

                 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
require(["geoscene/Map", "geoscene/views/MapView", "geoscene/Graphic"], (Map, MapView, Graphic) => {
  //
  // This is where you created a Map, MapView,
  // geometry, symbol, and attributes in the previous steps
  //

  // Create the graphic
  const polylineGraphic = new Graphic({
    geometry: polyline, // Add the geometry created in step 4
    symbol: lineSymbol, // Add the symbol created in step 5
    attributes: lineAtt // Add the attributes created in step 6
  });

  // Add the graphic to the view's default graphics view
  // If adding multiple graphics, use addMany and pass in the array of graphics.
  view.graphics.add(polylineGraphic);
});

7. 将 PopupTemplate 添加到图形

弹出窗口可以授予用户访问图形中所有属性的权限。设置简单的 PopupTemplate 将允许用户在视图中单击要素时查看要素的属性。有关如何使用弹出窗口PopupTemplate 的详细信息,请参阅弹出窗口简介教程和其他弹出窗口示例。

将新的 PopupTemplate 添加到图形中。此步骤必须在图形的构造函数中完成,或者在将图形添加到 GraphicsLayer 之前完成。

                                   
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
require(["geoscene/Map", "geoscene/views/MapView", "geoscene/Graphic"], (Map, MapView, Graphic) => {
  //
  // This is where you created a Map, MapView,
  // geometry, symbol, and attributes in the previous steps
  //

  // Create the graphic
  const polylineGraphic = new Graphic({
    geometry: polyline, // Add the geometry created in step 4
    symbol: lineSymbol, // Add the symbol created in step 5
    attributes: lineAtt, // Add the attributes created in step 6
    popupTemplate: {
      title: "{Name}",
      content: [
        {
          type: "fields",
          fieldInfos: [
            {
              fieldName: "Name"
            },
            {
              fieldName: "Owner"
            },
            {
              fieldName: "Length"
            }
          ]
        }
      ]
    }
  });

  // Add the graphic to the view's default graphics view
  view.graphics.add(polylineGraphic);
});

8. 结论

现在,您有了一个显示 Keystone 管道位置的简单地图。您可以单击该线并查看添加到图形中的属性。

您的最终 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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
require(["geoscene/Map", "geoscene/views/MapView", "geoscene/Graphic"], (Map, MapView, Graphic) => {
  const map = new Map({
    basemap: "hybrid"
  });

  const view = new MapView({
    center: [-80, 35],
    container: "viewDiv",
    map: map,
    zoom: 3
  });

  // First create a line geometry (this is the Keystone pipeline)
  const polyline = {
    type: "polyline", // autocasts as new Polyline()
    paths: [[-111.3, 52.68], [-98, 49.5], [-93.94, 29.89]]
  };

  // Create a symbol for drawing the line
  const lineSymbol = {
    type: "simple-line", // autocasts as SimpleLineSymbol()
    color: [226, 119, 40],
    width: 4
  };

  // Create an object for storing attributes related to the line
  const lineAtt = {
    Name: "Keystone Pipeline",
    Owner: "TransCanada",
    Length: "3,456 km"
  };

  /*******************************************
   * Create a new graphic and add the geometry,
   * symbol, and attributes to it. You may also
   * add a simple PopupTemplate to the graphic.
   * This allows users to view the graphic's
   * attributes when it is clicked.
   ******************************************/
  const polylineGraphic = new Graphic({
    geometry: polyline,
    symbol: lineSymbol,
    attributes: lineAtt,
    popupTemplate: {
      // autocasts as new PopupTemplate()
      title: "{Name}",
      content: [
        {
          type: "fields",
          fieldInfos: [
            {
              fieldName: "Name"
            },
            {
              fieldName: "Owner"
            },
            {
              fieldName: "Length"
            }
          ]
        }
      ]
    }
  });

  // Add the line graphic to the view's GraphicsLayer
  view.graphics.add(polylineGraphic);
});

在使用各种几何类型的要素时,向 GraphicsLayer 添加图形非常方便。但是,如果专门处理相同几何类型的图形,我们建议您使用客户端 FeatureLayer 来利用其渲染和查询功能。有关如何执行此操作的示例,请参阅使用客户端图形创建 FeatureLayer 示例。

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