查找长度和面积

了解如何计算几何的长度和面积。

您可使用 geometryEngine 计算线的长度并确定面的面积。测量值取决于为几何定义的坐标系 ( 或空间参考)。如果几何的空间参考是 Web Mercator (3857) 或 WGS84 (4326),则应使用测地线计算以考虑地球的曲率。如果空间参考与 Web Mercator (3857) 或 WGS84 (4326) 不同,则应使用基于欧氏距离的平面测量。

在本教程中,您将使用草绘微件在视图上绘制图形,并使用 geometryEngine 来计算测地线和平面长度及面积,以查看两个测量值之间的差异。

步骤

创建新 Pen

  1. 要开始使用,请完成显示地图教程 使用此 Pen

设置 HTML

  1. 创建 measurements div 以显示计算结果,并添加一些 CSS 样式来设置字体大小和边距。

                                                                                                                                                                                                                 
    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
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
        <title>Get length and area</title>
        <style>
          html,
          body,
          #viewDiv {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
          }
    
          #measurements {
            padding: 4px 8px;
            font-size: 16px;
            bottom: 15px;
            left: 50%;
            margin-right: -50%;
            transform: translate(-50%,-50%);
          }
    
        </style>
    
      <link rel="stylesheet" href="https://js.geoscene.cn/4.23/geoscene/themes/light/main.css">
      <script src="https://js.geoscene.cn/4.23/"></script>
        <script>
          require([
            "geoscene/config",
            "geoscene/Map",
            "geoscene/views/MapView",
    
            "geoscene/widgets/ScaleBar",
            "geoscene/widgets/Sketch",
            "geoscene/Graphic",
            "geoscene/layers/GraphicsLayer",
            "geoscene/geometry/geometryEngine",
    
          ], (
            geosceneConfig,
            Map,
            MapView,
    
          ) => {
    
            const map = new Map({
              basemap: "tianditu-vector", // 底图图层服务
            });
    
            const view = new MapView({
              container: "viewDiv",
              map: map,
    
              center: [-10, 30],
              zoom: 3,
            const scalebar = new ScaleBar({
              view: view,
              unit: "metric"
            view.ui.add(scalebar, "bottom-right");
            const graphicsLayer = new GraphicsLayer();
            const sketch = new Sketch({
              layer: graphicsLayer,
              view: view,
              availableCreateTools: ["polyline", "polygon", "rectangle"],
              creationMode: "update",
              updateOnGraphicClick: true,
              visibleElements: {
                createTools: {
                  point: false,
                  circle: false
                selectionTools:{
                  "lasso-selection": false,
                  "rectangle-selection":false,
                settingsMenu: false,
                undoRedoMenu: false
            view.ui.add(sketch, "top-right");
            const measurements = document.getElementById("measurements");
            view.ui.add(measurements, "manual");
            const polygon = {
                type: "polygon",
                spatialReference: {
                  wkid: 3857,
                rings: [
                    [-4508069.082189632, 3599936.936171892],
                    [-4508069.082189632, 5478453.343307884],
                    [-2629552.6750536393, 5478453.343307884],
                    [-2629552.6750536393, 3599936.936171892],
                    [-4508069.082189632, 3599936.936171892],
              const simplePolygonSymbol = {
                type: "simple-fill",
                outline: {
                  color: [200, 0, 0],
                  width: 2,
              const polygonGraphic = new Graphic({
                geometry: polygon,
                symbol: simplePolygonSymbol
            view.when(() => {
            sketch.on("update", (e) => {
              const geometry = e.graphics[0].geometry;
              if (e.state === "start") {
              if (e.state === "complete") {
                graphicsLayer.remove(graphicsLayer.graphics.getItemAt(0));
                measurements.innerHTML = null;
              if (
                (e.toolEventInfo.type === "scale-stop" ||
                  e.toolEventInfo.type === "reshape-stop" ||
                  e.toolEventInfo.type === "move-stop")
            function getArea(polygon) {
              const geodesicArea = geometryEngine.geodesicArea(polygon, "square-kilometers");
              const planarArea = geometryEngine.planarArea(polygon, "square-kilometers");
              "<b>Geodesic area</b>:  " + geodesicArea.toFixed(2) + " km\xB2" + " |   <b>Planar area</b>: " + planarArea.toFixed(2) + "  km\xB2";
            function getLength(line) {
              const geodesicLength = geometryEngine.geodesicLength(line, "kilometers");
              const planarLength = geometryEngine.planarLength(line, "kilometers");
                "<b>Geodesic length</b>:  " + geodesicLength.toFixed(2) + " km" + " |   <b>Planar length</b>: " + planarLength.toFixed(2) + "  km";
            function switchType(geom) {
              switch (geom.type) {
                case "polygon":
                  break;
                case "polyline":
                  break;
                default:
                  console.log("No value found");
          });
        </script>
      </head>
      <body>
        <div id="viewDiv">
    
          <div id="measurements" class="geoscene-widget">
    
        </div>
      </div>
      </body>
    </html>

添加模块

  1. 在 require 语句中,添加模块。

    GeoScene API for JavaScript 使用 AMD 模块require 函数用于加载模块,以便它们可在主 function 中使用。保持模块引用和函数参数的顺序相同很重要。

                                                                                                                                                                                                                 
    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
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
        <title>Get length and area</title>
        <style>
          html,
          body,
          #viewDiv {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
          #measurements {
            padding: 4px 8px;
            font-size: 16px;
            bottom: 15px;
            left: 50%;
            margin-right: -50%;
            transform: translate(-50%,-50%);
        </style>
    
      <link rel="stylesheet" href="https://js.geoscene.cn/4.23/geoscene/themes/light/main.css">
      <script src="https://js.geoscene.cn/4.23/"></script>
        <script>
          require([
            "geoscene/config",
            "geoscene/Map",
            "geoscene/views/MapView",
    
            "geoscene/widgets/ScaleBar",
            "geoscene/widgets/Sketch",
            "geoscene/Graphic",
            "geoscene/layers/GraphicsLayer",
            "geoscene/geometry/geometryEngine",
    
          ], (
            geosceneConfig,
            Map,
            MapView,
    
            ScaleBar,
            Sketch,
            Graphic,
            GraphicsLayer,
            geometryEngine,
    
          ) => {
    
            const map = new Map({
              basemap: "tianditu-vector", // 底图图层服务
            });
    
            const view = new MapView({
              container: "viewDiv",
              map: map,
    
              center: [-10, 30],
              zoom: 3,
            const scalebar = new ScaleBar({
              view: view,
              unit: "metric"
            view.ui.add(scalebar, "bottom-right");
            const graphicsLayer = new GraphicsLayer();
            const sketch = new Sketch({
              layer: graphicsLayer,
              view: view,
              availableCreateTools: ["polyline", "polygon", "rectangle"],
              creationMode: "update",
              updateOnGraphicClick: true,
              visibleElements: {
                createTools: {
                  point: false,
                  circle: false
                selectionTools:{
                  "lasso-selection": false,
                  "rectangle-selection":false,
                settingsMenu: false,
                undoRedoMenu: false
            view.ui.add(sketch, "top-right");
            const measurements = document.getElementById("measurements");
            view.ui.add(measurements, "manual");
            const polygon = {
                type: "polygon",
                spatialReference: {
                  wkid: 3857,
                rings: [
                    [-4508069.082189632, 3599936.936171892],
                    [-4508069.082189632, 5478453.343307884],
                    [-2629552.6750536393, 5478453.343307884],
                    [-2629552.6750536393, 3599936.936171892],
                    [-4508069.082189632, 3599936.936171892],
              const simplePolygonSymbol = {
                type: "simple-fill",
                outline: {
                  color: [200, 0, 0],
                  width: 2,
              const polygonGraphic = new Graphic({
                geometry: polygon,
                symbol: simplePolygonSymbol
            view.when(() => {
            sketch.on("update", (e) => {
              const geometry = e.graphics[0].geometry;
              if (e.state === "start") {
              if (e.state === "complete") {
                graphicsLayer.remove(graphicsLayer.graphics.getItemAt(0));
                measurements.innerHTML = null;
              if (
                (e.toolEventInfo.type === "scale-stop" ||
                  e.toolEventInfo.type === "reshape-stop" ||
                  e.toolEventInfo.type === "move-stop")
            function getArea(polygon) {
              const geodesicArea = geometryEngine.geodesicArea(polygon, "square-kilometers");
              const planarArea = geometryEngine.planarArea(polygon, "square-kilometers");
              "<b>Geodesic area</b>:  " + geodesicArea.toFixed(2) + " km\xB2" + " |   <b>Planar area</b>: " + planarArea.toFixed(2) + "  km\xB2";
            function getLength(line) {
              const geodesicLength = geometryEngine.geodesicLength(line, "kilometers");
              const planarLength = geometryEngine.planarLength(line, "kilometers");
                "<b>Geodesic length</b>:  " + geodesicLength.toFixed(2) + " km" + " |   <b>Planar length</b>: " + planarLength.toFixed(2) + "  km";
            function switchType(geom) {
              switch (geom.type) {
                case "polygon":
                  break;
                case "polyline":
                  break;
                default:
                  console.log("No value found");
        </script>
      </head>
      <body>
        <div id="viewDiv">
    
          <div id="measurements" class="geoscene-widget">
    
        </div>
      </div>
      </body>
    </html>

重定位并添加比例尺

ScaleBar 微件将在地图上显示比例尺。您可以选择公制或非公制值。例如,如果指定 metric,则会根据比例显示公里或米。

  1. 更改 viewcenterzoom 级别。

                                                                                                                                                                                                                 
    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
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
        <title>Get length and area</title>
        <style>
          html,
          body,
          #viewDiv {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
          #measurements {
            padding: 4px 8px;
            font-size: 16px;
            bottom: 15px;
            left: 50%;
            margin-right: -50%;
            transform: translate(-50%,-50%);
        </style>
    
      <link rel="stylesheet" href="https://js.geoscene.cn/4.23/geoscene/themes/light/main.css">
      <script src="https://js.geoscene.cn/4.23/"></script>
        <script>
          require([
            "geoscene/config",
            "geoscene/Map",
            "geoscene/views/MapView",
            "geoscene/widgets/ScaleBar",
            "geoscene/widgets/Sketch",
            "geoscene/Graphic",
            "geoscene/layers/GraphicsLayer",
            "geoscene/geometry/geometryEngine",
          ], (
            Map,
          ) => {
            const map = new Map({
              basemap: "tianditu-vector", // Basemap layer service
            const view = new MapView({
              container: "viewDiv",
              map: map,
    
              center: [-10, 30],
              zoom: 3,
    
            });
            const scalebar = new ScaleBar({
              view: view,
              unit: "metric"
            view.ui.add(scalebar, "bottom-right");
            const graphicsLayer = new GraphicsLayer();
            const sketch = new Sketch({
              layer: graphicsLayer,
              view: view,
              availableCreateTools: ["polyline", "polygon", "rectangle"],
              creationMode: "update",
              updateOnGraphicClick: true,
              visibleElements: {
                createTools: {
                  point: false,
                  circle: false
                selectionTools:{
                  "lasso-selection": false,
                  "rectangle-selection":false,
                settingsMenu: false,
                undoRedoMenu: false
            view.ui.add(sketch, "top-right");
            const measurements = document.getElementById("measurements");
            view.ui.add(measurements, "manual");
            const polygon = {
                type: "polygon",
                spatialReference: {
                  wkid: 3857,
                rings: [
                    [-4508069.082189632, 3599936.936171892],
                    [-4508069.082189632, 5478453.343307884],
                    [-2629552.6750536393, 5478453.343307884],
                    [-2629552.6750536393, 3599936.936171892],
                    [-4508069.082189632, 3599936.936171892],
              const simplePolygonSymbol = {
                type: "simple-fill",
                outline: {
                  color: [200, 0, 0],
                  width: 2,
              const polygonGraphic = new Graphic({
                geometry: polygon,
                symbol: simplePolygonSymbol
            view.when(() => {
            sketch.on("update", (e) => {
              const geometry = e.graphics[0].geometry;
              if (e.state === "start") {
              if (e.state === "complete") {
                graphicsLayer.remove(graphicsLayer.graphics.getItemAt(0));
                measurements.innerHTML = null;
              if (
                (e.toolEventInfo.type === "scale-stop" ||
                  e.toolEventInfo.type === "reshape-stop" ||
                  e.toolEventInfo.type === "move-stop")
            function getArea(polygon) {
              const geodesicArea = geometryEngine.geodesicArea(polygon, "square-kilometers");
              const planarArea = geometryEngine.planarArea(polygon, "square-kilometers");
              "<b>Geodesic area</b>:  " + geodesicArea.toFixed(2) + " km\xB2" + " |   <b>Planar area</b>: " + planarArea.toFixed(2) + "  km\xB2";
            function getLength(line) {
              const geodesicLength = geometryEngine.geodesicLength(line, "kilometers");
              const planarLength = geometryEngine.planarLength(line, "kilometers");
                "<b>Geodesic length</b>:  " + geodesicLength.toFixed(2) + " km" + " |   <b>Planar length</b>: " + planarLength.toFixed(2) + "  km";
            function switchType(geom) {
              switch (geom.type) {
                case "polygon":
                  break;
                case "polyline":
                  break;
                default:
                  console.log("No value found");
        </script>
      </head>
      <body>
        <div id="viewDiv">
    
          <div id="measurements" class="geoscene-widget">
    
        </div>
      </div>
      </body>
    </html>
  2. 创建 scalebar 并将 view 参数设置为 view。将 unit 指定为 metric

                                                                                                                                                                                                                 
    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
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
        <title>Get length and area</title>
        <style>
          html,
          body,
          #viewDiv {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
          #measurements {
            padding: 4px 8px;
            font-size: 16px;
            bottom: 15px;
            left: 50%;
            margin-right: -50%;
            transform: translate(-50%,-50%);
        </style>
    
      <link rel="stylesheet" href="https://js.geoscene.cn/4.23/geoscene/themes/light/main.css">
      <script src="https://js.geoscene.cn/4.23/"></script>
        <script>
          require([
            "geoscene/config",
            "geoscene/Map",
            "geoscene/views/MapView",
            "geoscene/widgets/ScaleBar",
            "geoscene/widgets/Sketch",
            "geoscene/Graphic",
            "geoscene/layers/GraphicsLayer",
            "geoscene/geometry/geometryEngine",
          ], (
            Map,
          ) => {
            const map = new Map({
              basemap: "tianditu-vector", // Basemap layer service
            const view = new MapView({
              container: "viewDiv",
              map: map,
              center: [-10, 30],
              zoom: 3,
    
            });
    
            const scalebar = new ScaleBar({
              view: view,
              unit: "metric"
            });
    
            view.ui.add(scalebar, "bottom-right");
            const graphicsLayer = new GraphicsLayer();
            const sketch = new Sketch({
              layer: graphicsLayer,
              view: view,
              availableCreateTools: ["polyline", "polygon", "rectangle"],
              creationMode: "update",
              updateOnGraphicClick: true,
              visibleElements: {
                createTools: {
                  point: false,
                  circle: false
                selectionTools:{
                  "lasso-selection": false,
                  "rectangle-selection":false,
                settingsMenu: false,
                undoRedoMenu: false
            view.ui.add(sketch, "top-right");
            const measurements = document.getElementById("measurements");
            view.ui.add(measurements, "manual");
            const polygon = {
                type: "polygon",
                spatialReference: {
                  wkid: 3857,
                rings: [
                    [-4508069.082189632, 3599936.936171892],
                    [-4508069.082189632, 5478453.343307884],
                    [-2629552.6750536393, 5478453.343307884],
                    [-2629552.6750536393, 3599936.936171892],
                    [-4508069.082189632, 3599936.936171892],
              const simplePolygonSymbol = {
                type: "simple-fill",
                outline: {
                  color: [200, 0, 0],
                  width: 2,
              const polygonGraphic = new Graphic({
                geometry: polygon,
                symbol: simplePolygonSymbol
            view.when(() => {
            sketch.on("update", (e) => {
              const geometry = e.graphics[0].geometry;
              if (e.state === "start") {
              if (e.state === "complete") {
                graphicsLayer.remove(graphicsLayer.graphics.getItemAt(0));
                measurements.innerHTML = null;
              if (
                (e.toolEventInfo.type === "scale-stop" ||
                  e.toolEventInfo.type === "reshape-stop" ||
                  e.toolEventInfo.type === "move-stop")
            function getArea(polygon) {
              const geodesicArea = geometryEngine.geodesicArea(polygon, "square-kilometers");
              const planarArea = geometryEngine.planarArea(polygon, "square-kilometers");
              "<b>Geodesic area</b>:  " + geodesicArea.toFixed(2) + " km\xB2" + " |   <b>Planar area</b>: " + planarArea.toFixed(2) + "  km\xB2";
            function getLength(line) {
              const geodesicLength = geometryEngine.geodesicLength(line, "kilometers");
              const planarLength = geometryEngine.planarLength(line, "kilometers");
                "<b>Geodesic length</b>:  " + geodesicLength.toFixed(2) + " km" + " |   <b>Planar length</b>: " + planarLength.toFixed(2) + "  km";
            function switchType(geom) {
              switch (geom.type) {
                case "polygon":
                  break;
                case "polyline":
                  break;
                default:
                  console.log("No value found");
        </script>
      </head>
      <body>
        <div id="viewDiv">
    
          <div id="measurements" class="geoscene-widget">
    
        </div>
      </div>
      </body>
    </html>
  3. scalebar 添加至 viewbottom-right

                                                                                                                                                                                                                 
    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
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
        <title>Get length and area</title>
        <style>
          html,
          body,
          #viewDiv {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
          #measurements {
            padding: 4px 8px;
            font-size: 16px;
            bottom: 15px;
            left: 50%;
            margin-right: -50%;
            transform: translate(-50%,-50%);
        </style>
    
      <link rel="stylesheet" href="https://js.geoscene.cn/4.23/geoscene/themes/light/main.css">
      <script src="https://js.geoscene.cn/4.23/"></script>
        <script>
          require([
            "geoscene/config",
            "geoscene/Map",
            "geoscene/views/MapView",
            "geoscene/widgets/ScaleBar",
            "geoscene/widgets/Sketch",
            "geoscene/Graphic",
            "geoscene/layers/GraphicsLayer",
            "geoscene/geometry/geometryEngine",
          ], (
            Map,
          ) => {
            const map = new Map({
              basemap: "tianditu-vector", // Basemap layer service
            const view = new MapView({
              container: "viewDiv",
              map: map,
              center: [-10, 30],
              zoom: 3,
    
            });
    
            const scalebar = new ScaleBar({
              view: view,
              unit: "metric"
            });
    
            view.ui.add(scalebar, "bottom-right");
            const graphicsLayer = new GraphicsLayer();
            const sketch = new Sketch({
              layer: graphicsLayer,
              view: view,
              availableCreateTools: ["polyline", "polygon", "rectangle"],
              creationMode: "update",
              updateOnGraphicClick: true,
              visibleElements: {
                createTools: {
                  point: false,
                  circle: false
                selectionTools:{
                  "lasso-selection": false,
                  "rectangle-selection":false,
                settingsMenu: false,
                undoRedoMenu: false
            view.ui.add(sketch, "top-right");
            const measurements = document.getElementById("measurements");
            view.ui.add(measurements, "manual");
            const polygon = {
                type: "polygon",
                spatialReference: {
                  wkid: 3857,
                rings: [
                    [-4508069.082189632, 3599936.936171892],
                    [-4508069.082189632, 5478453.343307884],
                    [-2629552.6750536393, 5478453.343307884],
                    [-2629552.6750536393, 3599936.936171892],
                    [-4508069.082189632, 3599936.936171892],
              const simplePolygonSymbol = {
                type: "simple-fill",
                outline: {
                  color: [200, 0, 0],
                  width: 2,
              const polygonGraphic = new Graphic({
                geometry: polygon,
                symbol: simplePolygonSymbol
            view.when(() => {
            sketch.on("update", (e) => {
              const geometry = e.graphics[0].geometry;
              if (e.state === "start") {
              if (e.state === "complete") {
                graphicsLayer.remove(graphicsLayer.graphics.getItemAt(0));
                measurements.innerHTML = null;
              if (
                (e.toolEventInfo.type === "scale-stop" ||
                  e.toolEventInfo.type === "reshape-stop" ||
                  e.toolEventInfo.type === "move-stop")
            function getArea(polygon) {
              const geodesicArea = geometryEngine.geodesicArea(polygon, "square-kilometers");
              const planarArea = geometryEngine.planarArea(polygon, "square-kilometers");
              "<b>Geodesic area</b>:  " + geodesicArea.toFixed(2) + " km\xB2" + " |   <b>Planar area</b>: " + planarArea.toFixed(2) + "  km\xB2";
            function getLength(line) {
              const geodesicLength = geometryEngine.geodesicLength(line, "kilometers");
              const planarLength = geometryEngine.planarLength(line, "kilometers");
                "<b>Geodesic length</b>:  " + geodesicLength.toFixed(2) + " km" + " |   <b>Planar length</b>: " + planarLength.toFixed(2) + "  km";
            function switchType(geom) {
              switch (geom.type) {
                case "polygon":
                  break;
                case "polyline":
                  break;
                default:
                  console.log("No value found");
        </script>
      </head>
      <body>
        <div id="viewDiv">
    
          <div id="measurements" class="geoscene-widget">
    
        </div>
      </div>
      </body>
    </html>
  4. 运行应用程序以验证 centerzoom 级别中的更改。scalebar 应显示在视图的底部。

添加草绘微件

Sketch 微件提供的 UI 允许您在 MapView 中创建和更新图形

  1. 创建 graphicsLayer 以将 GraphicsLayer 添加到 map

                                                                                                                                                                                                                 
    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
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
        <title>Get length and area</title>
        <style>
          html,
          body,
          #viewDiv {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
          #measurements {
            padding: 4px 8px;
            font-size: 16px;
            bottom: 15px;
            left: 50%;
            margin-right: -50%;
            transform: translate(-50%,-50%);
        </style>
    
      <link rel="stylesheet" href="https://js.geoscene.cn/4.23/geoscene/themes/light/main.css">
      <script src="https://js.geoscene.cn/4.23/"></script>
        <script>
          require([
            "geoscene/config",
            "geoscene/Map",
            "geoscene/views/MapView",
            "geoscene/widgets/ScaleBar",
            "geoscene/widgets/Sketch",
            "geoscene/Graphic",
            "geoscene/layers/GraphicsLayer",
            "geoscene/geometry/geometryEngine",
          ], (
            Map,
          ) => {
            const map = new Map({
              basemap: "tianditu-vector", // Basemap layer service
            const view = new MapView({
              container: "viewDiv",
              map: map,
              center: [-10, 30],
              zoom: 3,
            const scalebar = new ScaleBar({
              view: view,
              unit: "metric"
            });
    
            view.ui.add(scalebar, "bottom-right");
    
            const graphicsLayer = new GraphicsLayer();
            map.add(graphicsLayer);
    
            const sketch = new Sketch({
              layer: graphicsLayer,
              view: view,
              availableCreateTools: ["polyline", "polygon", "rectangle"],
              creationMode: "update",
              updateOnGraphicClick: true,
              visibleElements: {
                createTools: {
                  point: false,
                  circle: false
                selectionTools:{
                  "lasso-selection": false,
                  "rectangle-selection":false,
                settingsMenu: false,
                undoRedoMenu: false
            view.ui.add(sketch, "top-right");
            const measurements = document.getElementById("measurements");
            view.ui.add(measurements, "manual");
            const polygon = {
                type: "polygon",
                spatialReference: {
                  wkid: 3857,
                rings: [
                    [-4508069.082189632, 3599936.936171892],
                    [-4508069.082189632, 5478453.343307884],
                    [-2629552.6750536393, 5478453.343307884],
                    [-2629552.6750536393, 3599936.936171892],
                    [-4508069.082189632, 3599936.936171892],
              const simplePolygonSymbol = {
                type: "simple-fill",
                outline: {
                  color: [200, 0, 0],
                  width: 2,
              const polygonGraphic = new Graphic({
                geometry: polygon,
                symbol: simplePolygonSymbol
            view.when(() => {
            sketch.on("update", (e) => {
              const geometry = e.graphics[0].geometry;
              if (e.state === "start") {
              if (e.state === "complete") {
                graphicsLayer.remove(graphicsLayer.graphics.getItemAt(0));
                measurements.innerHTML = null;
              if (
                (e.toolEventInfo.type === "scale-stop" ||
                  e.toolEventInfo.type === "reshape-stop" ||
                  e.toolEventInfo.type === "move-stop")
            function getArea(polygon) {
              const geodesicArea = geometryEngine.geodesicArea(polygon, "square-kilometers");
              const planarArea = geometryEngine.planarArea(polygon, "square-kilometers");
              "<b>Geodesic area</b>:  " + geodesicArea.toFixed(2) + " km\xB2" + " |   <b>Planar area</b>: " + planarArea.toFixed(2) + "  km\xB2";
            function getLength(line) {
              const geodesicLength = geometryEngine.geodesicLength(line, "kilometers");
              const planarLength = geometryEngine.planarLength(line, "kilometers");
                "<b>Geodesic length</b>:  " + geodesicLength.toFixed(2) + " km" + " |   <b>Planar length</b>: " + planarLength.toFixed(2) + "  km";
            function switchType(geom) {
              switch (geom.type) {
                case "polygon":
                  break;
                case "polyline":
                  break;
                default:
                  console.log("No value found");
        </script>
      </head>
      <body>
        <div id="viewDiv">
    
          <div id="measurements" class="geoscene-widget">
    
        </div>
      </div>
      </body>
    </html>
  2. 创建 sketch 微件。将 layer 参数设置为 graphicsLayer。使用微件绘制的任何几何都将添加到 graphicsLayer 中。通过将 availableCreateTools 设置为仅显示 polylinepolygonrectangle 选项并禁用在 visibleElementsselectionTools 属性中找到的其他默认设置来限制微件上的要素。

                                                                                                                                                                                                                 
    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
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
        <title>Get length and area</title>
        <style>
          html,
          body,
          #viewDiv {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
          #measurements {
            padding: 4px 8px;
            font-size: 16px;
            bottom: 15px;
            left: 50%;
            margin-right: -50%;
            transform: translate(-50%,-50%);
        </style>
    
      <link rel="stylesheet" href="https://js.geoscene.cn/4.23/geoscene/themes/light/main.css">
      <script src="https://js.geoscene.cn/4.23/"></script>
        <script>
          require([
            "geoscene/config",
            "geoscene/Map",
            "geoscene/views/MapView",
            "geoscene/widgets/ScaleBar",
            "geoscene/widgets/Sketch",
            "geoscene/Graphic",
            "geoscene/layers/GraphicsLayer",
            "geoscene/geometry/geometryEngine",
          ], (
            Map,
          ) => {
            const map = new Map({
              basemap: "tianditu-vector", // Basemap layer service
            const view = new MapView({
              container: "viewDiv",
              map: map,
              center: [-10, 30],
              zoom: 3,
            const scalebar = new ScaleBar({
              view: view,
              unit: "metric"
            });
    
            view.ui.add(scalebar, "bottom-right");
    
            const graphicsLayer = new GraphicsLayer();
            map.add(graphicsLayer);
    
            const sketch = new Sketch({
              layer: graphicsLayer,
              view: view,
              availableCreateTools: ["polyline", "polygon", "rectangle"],
              creationMode: "update",
              updateOnGraphicClick: true,
              visibleElements: {
                createTools: {
                  point: false,
                  circle: false
                },
                selectionTools:{
                  "lasso-selection": false,
                  "rectangle-selection":false,
                },
                settingsMenu: false,
                undoRedoMenu: false
              }
            });
    
            view.ui.add(sketch, "top-right");
            const measurements = document.getElementById("measurements");
            view.ui.add(measurements, "manual");
            const polygon = {
                type: "polygon",
                spatialReference: {
                  wkid: 3857,
                rings: [
                    [-4508069.082189632, 3599936.936171892],
                    [-4508069.082189632, 5478453.343307884],
                    [-2629552.6750536393, 5478453.343307884],
                    [-2629552.6750536393, 3599936.936171892],
                    [-4508069.082189632, 3599936.936171892],
              const simplePolygonSymbol = {
                type: "simple-fill",
                outline: {
                  color: [200, 0, 0],
                  width: 2,
              const polygonGraphic = new Graphic({
                geometry: polygon,
                symbol: simplePolygonSymbol
            view.when(() => {
            sketch.on("update", (e) => {
              const geometry = e.graphics[0].geometry;
              if (e.state === "start") {
              if (e.state === "complete") {
                graphicsLayer.remove(graphicsLayer.graphics.getItemAt(0));
                measurements.innerHTML = null;
              if (
                (e.toolEventInfo.type === "scale-stop" ||
                  e.toolEventInfo.type === "reshape-stop" ||
                  e.toolEventInfo.type === "move-stop")
            function getArea(polygon) {
              const geodesicArea = geometryEngine.geodesicArea(polygon, "square-kilometers");
              const planarArea = geometryEngine.planarArea(polygon, "square-kilometers");
              "<b>Geodesic area</b>:  " + geodesicArea.toFixed(2) + " km\xB2" + " |   <b>Planar area</b>: " + planarArea.toFixed(2) + "  km\xB2";
            function getLength(line) {
              const geodesicLength = geometryEngine.geodesicLength(line, "kilometers");
              const planarLength = geometryEngine.planarLength(line, "kilometers");
                "<b>Geodesic length</b>:  " + geodesicLength.toFixed(2) + " km" + " |   <b>Planar length</b>: " + planarLength.toFixed(2) + "  km";
            function switchType(geom) {
              switch (geom.type) {
                case "polygon":
                  break;
                case "polyline":
                  break;
                default:
                  console.log("No value found");
        </script>
      </head>
      <body>
        <div id="viewDiv">
    
          <div id="measurements" class="geoscene-widget">
    
        </div>
      </div>
      </body>
    </html>
  3. sketch 微件添加到 viewtop-right 处。

                                                                                                                                                                                                                 
    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
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
        <title>Get length and area</title>
        <style>
          html,
          body,
          #viewDiv {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
          #measurements {
            padding: 4px 8px;
            font-size: 16px;
            bottom: 15px;
            left: 50%;
            margin-right: -50%;
            transform: translate(-50%,-50%);
        </style>
    
      <link rel="stylesheet" href="https://js.geoscene.cn/4.23/geoscene/themes/light/main.css">
      <script src="https://js.geoscene.cn/4.23/"></script>
        <script>
          require([
            "geoscene/config",
            "geoscene/Map",
            "geoscene/views/MapView",
            "geoscene/widgets/ScaleBar",
            "geoscene/widgets/Sketch",
            "geoscene/Graphic",
            "geoscene/layers/GraphicsLayer",
            "geoscene/geometry/geometryEngine",
          ], (
            Map,
          ) => {
            const map = new Map({
              basemap: "tianditu-vector", // Basemap layer service
            const view = new MapView({
              container: "viewDiv",
              map: map,
              center: [-10, 30],
              zoom: 3,
            const scalebar = new ScaleBar({
              view: view,
              unit: "metric"
            view.ui.add(scalebar, "bottom-right");
            const graphicsLayer = new GraphicsLayer();
            const sketch = new Sketch({
              layer: graphicsLayer,
              view: view,
              availableCreateTools: ["polyline", "polygon", "rectangle"],
              creationMode: "update",
              updateOnGraphicClick: true,
              visibleElements: {
                createTools: {
                  point: false,
                  circle: false
                },
                selectionTools:{
                  "lasso-selection": false,
                  "rectangle-selection":false,
                },
                settingsMenu: false,
                undoRedoMenu: false
              }
            });
    
            view.ui.add(sketch, "top-right");
            const measurements = document.getElementById("measurements");
            view.ui.add(measurements, "manual");
            const polygon = {
                type: "polygon",
                spatialReference: {
                  wkid: 3857,
                rings: [
                    [-4508069.082189632, 3599936.936171892],
                    [-4508069.082189632, 5478453.343307884],
                    [-2629552.6750536393, 5478453.343307884],
                    [-2629552.6750536393, 3599936.936171892],
                    [-4508069.082189632, 3599936.936171892],
              const simplePolygonSymbol = {
                type: "simple-fill",
                outline: {
                  color: [200, 0, 0],
                  width: 2,
              const polygonGraphic = new Graphic({
                geometry: polygon,
                symbol: simplePolygonSymbol
            view.when(() => {
            sketch.on("update", (e) => {
              const geometry = e.graphics[0].geometry;
              if (e.state === "start") {
              if (e.state === "complete") {
                graphicsLayer.remove(graphicsLayer.graphics.getItemAt(0));
                measurements.innerHTML = null;
              if (
                (e.toolEventInfo.type === "scale-stop" ||
                  e.toolEventInfo.type === "reshape-stop" ||
                  e.toolEventInfo.type === "move-stop")
            function getArea(polygon) {
              const geodesicArea = geometryEngine.geodesicArea(polygon, "square-kilometers");
              const planarArea = geometryEngine.planarArea(polygon, "square-kilometers");
              "<b>Geodesic area</b>:  " + geodesicArea.toFixed(2) + " km\xB2" + " |   <b>Planar area</b>: " + planarArea.toFixed(2) + "  km\xB2";
            function getLength(line) {
              const geodesicLength = geometryEngine.geodesicLength(line, "kilometers");
              const planarLength = geometryEngine.planarLength(line, "kilometers");
                "<b>Geodesic length</b>:  " + geodesicLength.toFixed(2) + " km" + " |   <b>Planar length</b>: " + planarLength.toFixed(2) + "  km";
            function switchType(geom) {
              switch (geom.type) {
                case "polygon":
                  break;
                case "polyline":
                  break;
                default:
                  console.log("No value found");
        </script>
      </head>
      <body>
        <div id="viewDiv">
    
          <div id="measurements" class="geoscene-widget">
    
        </div>
      </div>
      </body>
    </html>
  4. measurements 元素添加到 view 中以在绘制几何时显示测量值。

                                                                                                                                                                                                                 
    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
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
        <title>Get length and area</title>
        <style>
          html,
          body,
          #viewDiv {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
          #measurements {
            padding: 4px 8px;
            font-size: 16px;
            bottom: 15px;
            left: 50%;
            margin-right: -50%;
            transform: translate(-50%,-50%);
        </style>
    
      <link rel="stylesheet" href="https://js.geoscene.cn/4.23/geoscene/themes/light/main.css">
      <script src="https://js.geoscene.cn/4.23/"></script>
        <script>
          require([
            "geoscene/config",
            "geoscene/Map",
            "geoscene/views/MapView",
            "geoscene/widgets/ScaleBar",
            "geoscene/widgets/Sketch",
            "geoscene/Graphic",
            "geoscene/layers/GraphicsLayer",
            "geoscene/geometry/geometryEngine",
          ], (
            Map,
          ) => {
            const map = new Map({
              basemap: "tianditu-vector", // Basemap layer service
            const view = new MapView({
              container: "viewDiv",
              map: map,
              center: [-10, 30],
              zoom: 3,
            const scalebar = new ScaleBar({
              view: view,
              unit: "metric"
            view.ui.add(scalebar, "bottom-right");
            const graphicsLayer = new GraphicsLayer();
            const sketch = new Sketch({
              layer: graphicsLayer,
              view: view,
              availableCreateTools: ["polyline", "polygon", "rectangle"],
              creationMode: "update",
              updateOnGraphicClick: true,
              visibleElements: {
                createTools: {
                  point: false,
                  circle: false
                selectionTools:{
                  "lasso-selection": false,
                  "rectangle-selection":false,
                settingsMenu: false,
                undoRedoMenu: false
            view.ui.add(sketch, "top-right");
    
            const measurements = document.getElementById("measurements");
            view.ui.add(measurements, "manual");
            const polygon = {
                type: "polygon",
                spatialReference: {
                  wkid: 3857,
                rings: [
                    [-4508069.082189632, 3599936.936171892],
                    [-4508069.082189632, 5478453.343307884],
                    [-2629552.6750536393, 5478453.343307884],
                    [-2629552.6750536393, 3599936.936171892],
                    [-4508069.082189632, 3599936.936171892],
              const simplePolygonSymbol = {
                type: "simple-fill",
                outline: {
                  color: [200, 0, 0],
                  width: 2,
              const polygonGraphic = new Graphic({
                geometry: polygon,
                symbol: simplePolygonSymbol
            view.when(() => {
            sketch.on("update", (e) => {
              const geometry = e.graphics[0].geometry;
              if (e.state === "start") {
              if (e.state === "complete") {
                graphicsLayer.remove(graphicsLayer.graphics.getItemAt(0));
                measurements.innerHTML = null;
              if (
                (e.toolEventInfo.type === "scale-stop" ||
                  e.toolEventInfo.type === "reshape-stop" ||
                  e.toolEventInfo.type === "move-stop")
            function getArea(polygon) {
              const geodesicArea = geometryEngine.geodesicArea(polygon, "square-kilometers");
              const planarArea = geometryEngine.planarArea(polygon, "square-kilometers");
              "<b>Geodesic area</b>:  " + geodesicArea.toFixed(2) + " km\xB2" + " |   <b>Planar area</b>: " + planarArea.toFixed(2) + "  km\xB2";
            function getLength(line) {
              const geodesicLength = geometryEngine.geodesicLength(line, "kilometers");
              const planarLength = geometryEngine.planarLength(line, "kilometers");
                "<b>Geodesic length</b>:  " + geodesicLength.toFixed(2) + " km" + " |   <b>Planar length</b>: " + planarLength.toFixed(2) + "  km";
            function switchType(geom) {
              switch (geom.type) {
                case "polygon":
                  break;
                case "polyline":
                  break;
                default:
                  console.log("No value found");
        </script>
      </head>
      <body>
        <div id="viewDiv">
    
          <div id="measurements" class="geoscene-widget">
    
        </div>
      </div>
      </body>
    </html>
  5. 运行应用程序以验证微件是否显示在视图中,以及您是否能够绘制几何。

计算长度和面积

geometryEngine 允许您计算几何的平面长度/面积或测地线长度/面积。由于此应用程序中的几何使用 Web Mercator 进行投影,因此最佳做法是使用测地线测量。但是,要可视化测地线计算和平面计算之间的差异,请在绘制几何时同时测量这两者。

  1. 创建 getArea 函数,该函数使用 polygon 作为其参数。调用 geodesicArea() 方法和 planarArea() 方法来计算多边形的面积,以 square-kilometers 为单位。

                                                                                                                                                                                                                 
    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
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
        <title>Get length and area</title>
        <style>
          html,
          body,
          #viewDiv {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
          #measurements {
            padding: 4px 8px;
            font-size: 16px;
            bottom: 15px;
            left: 50%;
            margin-right: -50%;
            transform: translate(-50%,-50%);
        </style>
    
      <link rel="stylesheet" href="https://js.geoscene.cn/4.23/geoscene/themes/light/main.css">
      <script src="https://js.geoscene.cn/4.23/"></script>
        <script>
          require([
            "geoscene/config",
            "geoscene/Map",
            "geoscene/views/MapView",
            "geoscene/widgets/ScaleBar",
            "geoscene/widgets/Sketch",
            "geoscene/Graphic",
            "geoscene/layers/GraphicsLayer",
            "geoscene/geometry/geometryEngine",
          ], (
            Map,
          ) => {
            const map = new Map({
              basemap: "tianditu-vector", // Basemap layer service
            const view = new MapView({
              container: "viewDiv",
              map: map,
              center: [-10, 30],
              zoom: 3,
            const scalebar = new ScaleBar({
              view: view,
              unit: "metric"
            view.ui.add(scalebar, "bottom-right");
            const graphicsLayer = new GraphicsLayer();
            const sketch = new Sketch({
              layer: graphicsLayer,
              view: view,
              availableCreateTools: ["polyline", "polygon", "rectangle"],
              creationMode: "update",
              updateOnGraphicClick: true,
              visibleElements: {
                createTools: {
                  point: false,
                  circle: false
                selectionTools:{
                  "lasso-selection": false,
                  "rectangle-selection":false,
                settingsMenu: false,
                undoRedoMenu: false
            view.ui.add(sketch, "top-right");
            const measurements = document.getElementById("measurements");
            view.ui.add(measurements, "manual");
    
            const polygon = {
                type: "polygon",
                spatialReference: {
                  wkid: 3857,
                rings: [
                    [-4508069.082189632, 3599936.936171892],
                    [-4508069.082189632, 5478453.343307884],
                    [-2629552.6750536393, 5478453.343307884],
                    [-2629552.6750536393, 3599936.936171892],
                    [-4508069.082189632, 3599936.936171892],
              const simplePolygonSymbol = {
                type: "simple-fill",
                outline: {
                  color: [200, 0, 0],
                  width: 2,
              const polygonGraphic = new Graphic({
                geometry: polygon,
                symbol: simplePolygonSymbol
            view.when(() => {
            sketch.on("update", (e) => {
              const geometry = e.graphics[0].geometry;
              if (e.state === "start") {
              if (e.state === "complete") {
                graphicsLayer.remove(graphicsLayer.graphics.getItemAt(0));
                measurements.innerHTML = null;
              if (
                (e.toolEventInfo.type === "scale-stop" ||
                  e.toolEventInfo.type === "reshape-stop" ||
                  e.toolEventInfo.type === "move-stop")
            function getArea(polygon) {
              const geodesicArea = geometryEngine.geodesicArea(polygon, "square-kilometers");
              const planarArea = geometryEngine.planarArea(polygon, "square-kilometers");
    
              "<b>Geodesic area</b>:  " + geodesicArea.toFixed(2) + " km\xB2" + " |   <b>Planar area</b>: " + planarArea.toFixed(2) + "  km\xB2";
            }
    
            function getLength(line) {
              const geodesicLength = geometryEngine.geodesicLength(line, "kilometers");
              const planarLength = geometryEngine.planarLength(line, "kilometers");
                "<b>Geodesic length</b>:  " + geodesicLength.toFixed(2) + " km" + " |   <b>Planar length</b>: " + planarLength.toFixed(2) + "  km";
            function switchType(geom) {
              switch (geom.type) {
                case "polygon":
                  break;
                case "polyline":
                  break;
                default:
                  console.log("No value found");
        </script>
      </head>
      <body>
        <div id="viewDiv">
    
          <div id="measurements" class="geoscene-widget">
    
        </div>
      </div>
      </body>
    </html>
  2. 将计算结果追加到 measurementsinnerHTML

                                                                                                                                                                                                                 
    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
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
        <title>Get length and area</title>
        <style>
          html,
          body,
          #viewDiv {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
          #measurements {
            padding: 4px 8px;
            font-size: 16px;
            bottom: 15px;
            left: 50%;
            margin-right: -50%;
            transform: translate(-50%,-50%);
        </style>
    
      <link rel="stylesheet" href="https://js.geoscene.cn/4.23/geoscene/themes/light/main.css">
      <script src="https://js.geoscene.cn/4.23/"></script>
        <script>
          require([
            "geoscene/config",
            "geoscene/Map",
            "geoscene/views/MapView",
            "geoscene/widgets/ScaleBar",
            "geoscene/widgets/Sketch",
            "geoscene/Graphic",
            "geoscene/layers/GraphicsLayer",
            "geoscene/geometry/geometryEngine",
          ], (
            Map,
          ) => {
            const map = new Map({
              basemap: "tianditu-vector", // Basemap layer service
            const view = new MapView({
              container: "viewDiv",
              map: map,
              center: [-10, 30],
              zoom: 3,
            const scalebar = new ScaleBar({
              view: view,
              unit: "metric"
            view.ui.add(scalebar, "bottom-right");
            const graphicsLayer = new GraphicsLayer();
            const sketch = new Sketch({
              layer: graphicsLayer,
              view: view,
              availableCreateTools: ["polyline", "polygon", "rectangle"],
              creationMode: "update",
              updateOnGraphicClick: true,
              visibleElements: {
                createTools: {
                  point: false,
                  circle: false
                selectionTools:{
                  "lasso-selection": false,
                  "rectangle-selection":false,
                settingsMenu: false,
                undoRedoMenu: false
            view.ui.add(sketch, "top-right");
            const measurements = document.getElementById("measurements");
            view.ui.add(measurements, "manual");
            const polygon = {
                type: "polygon",
                spatialReference: {
                  wkid: 3857,
                rings: [
                    [-4508069.082189632, 3599936.936171892],
                    [-4508069.082189632, 5478453.343307884],
                    [-2629552.6750536393, 5478453.343307884],
                    [-2629552.6750536393, 3599936.936171892],
                    [-4508069.082189632, 3599936.936171892],
              const simplePolygonSymbol = {
                type: "simple-fill",
                outline: {
                  color: [200, 0, 0],
                  width: 2,
              const polygonGraphic = new Graphic({
                geometry: polygon,
                symbol: simplePolygonSymbol
            view.when(() => {
            sketch.on("update", (e) => {
              const geometry = e.graphics[0].geometry;
              if (e.state === "start") {
              if (e.state === "complete") {
                graphicsLayer.remove(graphicsLayer.graphics.getItemAt(0));
                measurements.innerHTML = null;
              if (
                (e.toolEventInfo.type === "scale-stop" ||
                  e.toolEventInfo.type === "reshape-stop" ||
                  e.toolEventInfo.type === "move-stop")
            function getArea(polygon) {
              const geodesicArea = geometryEngine.geodesicArea(polygon, "square-kilometers");
              const planarArea = geometryEngine.planarArea(polygon, "square-kilometers");
    
              measurements.innerHTML =
              "<b>Geodesic area</b>:  " + geodesicArea.toFixed(2) + " km\xB2" + " |   <b>Planar area</b>: " + planarArea.toFixed(2) + "  km\xB2";
    
            }
            function getLength(line) {
              const geodesicLength = geometryEngine.geodesicLength(line, "kilometers");
              const planarLength = geometryEngine.planarLength(line, "kilometers");
                "<b>Geodesic length</b>:  " + geodesicLength.toFixed(2) + " km" + " |   <b>Planar length</b>: " + planarLength.toFixed(2) + "  km";
            function switchType(geom) {
              switch (geom.type) {
                case "polygon":
                  break;
                case "polyline":
                  break;
                default:
                  console.log("No value found");
        </script>
      </head>
      <body>
        <div id="viewDiv">
    
          <div id="measurements" class="geoscene-widget">
    
        </div>
      </div>
      </body>
    </html>
  3. 创建 getLength 函数,该函数使用 line 参数。调用 geodesicLengthplanarLength 方法来计算 line 的长度,以千米为单位。

                                                                                                                                                                                                                 
    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
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
        <title>Get length and area</title>
        <style>
          html,
          body,
          #viewDiv {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
          #measurements {
            padding: 4px 8px;
            font-size: 16px;
            bottom: 15px;
            left: 50%;
            margin-right: -50%;
            transform: translate(-50%,-50%);
        </style>
    
      <link rel="stylesheet" href="https://js.geoscene.cn/4.23/geoscene/themes/light/main.css">
      <script src="https://js.geoscene.cn/4.23/"></script>
        <script>
          require([
            "geoscene/config",
            "geoscene/Map",
            "geoscene/views/MapView",
            "geoscene/widgets/ScaleBar",
            "geoscene/widgets/Sketch",
            "geoscene/Graphic",
            "geoscene/layers/GraphicsLayer",
            "geoscene/geometry/geometryEngine",
          ], (
            Map,
          ) => {
            const map = new Map({
              basemap: "tianditu-vector", // Basemap layer service
            const view = new MapView({
              container: "viewDiv",
              map: map,
              center: [-10, 30],
              zoom: 3,
            const scalebar = new ScaleBar({
              view: view,
              unit: "metric"
            view.ui.add(scalebar, "bottom-right");
            const graphicsLayer = new GraphicsLayer();
            const sketch = new Sketch({
              layer: graphicsLayer,
              view: view,
              availableCreateTools: ["polyline", "polygon", "rectangle"],
              creationMode: "update",
              updateOnGraphicClick: true,
              visibleElements: {
                createTools: {
                  point: false,
                  circle: false
                selectionTools:{
                  "lasso-selection": false,
                  "rectangle-selection":false,
                settingsMenu: false,
                undoRedoMenu: false
            view.ui.add(sketch, "top-right");
            const measurements = document.getElementById("measurements");
            view.ui.add(measurements, "manual");
            const polygon = {
                type: "polygon",
                spatialReference: {
                  wkid: 3857,
                rings: [
                    [-4508069.082189632, 3599936.936171892],
                    [-4508069.082189632, 5478453.343307884],
                    [-2629552.6750536393, 5478453.343307884],
                    [-2629552.6750536393, 3599936.936171892],
                    [-4508069.082189632, 3599936.936171892],
              const simplePolygonSymbol = {
                type: "simple-fill",
                outline: {
                  color: [200, 0, 0],
                  width: 2,
              const polygonGraphic = new Graphic({
                geometry: polygon,
                symbol: simplePolygonSymbol
            view.when(() => {
            sketch.on("update", (e) => {
              const geometry = e.graphics[0].geometry;
              if (e.state === "start") {
              if (e.state === "complete") {
                graphicsLayer.remove(graphicsLayer.graphics.getItemAt(0));
                measurements.innerHTML = null;
              if (
                (e.toolEventInfo.type === "scale-stop" ||
                  e.toolEventInfo.type === "reshape-stop" ||
                  e.toolEventInfo.type === "move-stop")
            function getArea(polygon) {
              const geodesicArea = geometryEngine.geodesicArea(polygon, "square-kilometers");
              const planarArea = geometryEngine.planarArea(polygon, "square-kilometers");
              measurements.innerHTML =
              "<b>Geodesic area</b>:  " + geodesicArea.toFixed(2) + " km\xB2" + " |   <b>Planar area</b>: " + planarArea.toFixed(2) + "  km\xB2";
    
            }
    
            function getLength(line) {
              const geodesicLength = geometryEngine.geodesicLength(line, "kilometers");
              const planarLength = geometryEngine.planarLength(line, "kilometers");
    
                "<b>Geodesic length</b>:  " + geodesicLength.toFixed(2) + " km" + " |   <b>Planar length</b>: " + planarLength.toFixed(2) + "  km";
            }
            function switchType(geom) {
              switch (geom.type) {
                case "polygon":
                  break;
                case "polyline":
                  break;
                default:
                  console.log("No value found");
        </script>
      </head>
      <body>
        <div id="viewDiv">
    
          <div id="measurements" class="geoscene-widget">
    
        </div>
      </div>
      </body>
    </html>
  4. 将测地线长度计算的结果追加到 measurementsinnerHTML

                                                                                                                                                                                                                 
    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
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
        <title>Get length and area</title>
        <style>
          html,
          body,
          #viewDiv {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
          #measurements {
            padding: 4px 8px;
            font-size: 16px;
            bottom: 15px;
            left: 50%;
            margin-right: -50%;
            transform: translate(-50%,-50%);
        </style>
    
      <link rel="stylesheet" href="https://js.geoscene.cn/4.23/geoscene/themes/light/main.css">
      <script src="https://js.geoscene.cn/4.23/"></script>
        <script>
          require([
            "geoscene/config",
            "geoscene/Map",
            "geoscene/views/MapView",
            "geoscene/widgets/ScaleBar",
            "geoscene/widgets/Sketch",
            "geoscene/Graphic",
            "geoscene/layers/GraphicsLayer",
            "geoscene/geometry/geometryEngine",
          ], (
            Map,
          ) => {
            const map = new Map({
              basemap: "tianditu-vector", // Basemap layer service
            const view = new MapView({
              container: "viewDiv",
              map: map,
              center: [-10, 30],
              zoom: 3,
            const scalebar = new ScaleBar({
              view: view,
              unit: "metric"
            view.ui.add(scalebar, "bottom-right");
            const graphicsLayer = new GraphicsLayer();
            const sketch = new Sketch({
              layer: graphicsLayer,
              view: view,
              availableCreateTools: ["polyline", "polygon", "rectangle"],
              creationMode: "update",
              updateOnGraphicClick: true,
              visibleElements: {
                createTools: {
                  point: false,
                  circle: false
                selectionTools:{
                  "lasso-selection": false,
                  "rectangle-selection":false,
                settingsMenu: false,
                undoRedoMenu: false
            view.ui.add(sketch, "top-right");
            const measurements = document.getElementById("measurements");
            view.ui.add(measurements, "manual");
            const polygon = {
                type: "polygon",
                spatialReference: {
                  wkid: 3857,
                rings: [
                    [-4508069.082189632, 3599936.936171892],
                    [-4508069.082189632, 5478453.343307884],
                    [-2629552.6750536393, 5478453.343307884],
                    [-2629552.6750536393, 3599936.936171892],
                    [-4508069.082189632, 3599936.936171892],
              const simplePolygonSymbol = {
                type: "simple-fill",
                outline: {
                  color: [200, 0, 0],
                  width: 2,
              const polygonGraphic = new Graphic({
                geometry: polygon,
                symbol: simplePolygonSymbol
            view.when(() => {
            sketch.on("update", (e) => {
              const geometry = e.graphics[0].geometry;
              if (e.state === "start") {
              if (e.state === "complete") {
                graphicsLayer.remove(graphicsLayer.graphics.getItemAt(0));
                measurements.innerHTML = null;
              if (
                (e.toolEventInfo.type === "scale-stop" ||
                  e.toolEventInfo.type === "reshape-stop" ||
                  e.toolEventInfo.type === "move-stop")
            function getArea(polygon) {
              const geodesicArea = geometryEngine.geodesicArea(polygon, "square-kilometers");
              const planarArea = geometryEngine.planarArea(polygon, "square-kilometers");
              "<b>Geodesic area</b>:  " + geodesicArea.toFixed(2) + " km\xB2" + " |   <b>Planar area</b>: " + planarArea.toFixed(2) + "  km\xB2";
            function getLength(line) {
              const geodesicLength = geometryEngine.geodesicLength(line, "kilometers");
              const planarLength = geometryEngine.planarLength(line, "kilometers");
    
              measurements.innerHTML =
                "<b>Geodesic length</b>:  " + geodesicLength.toFixed(2) + " km" + " |   <b>Planar length</b>: " + planarLength.toFixed(2) + "  km";
    
            }
            function switchType(geom) {
              switch (geom.type) {
                case "polygon":
                  break;
                case "polyline":
                  break;
                default:
                  console.log("No value found");
        </script>
      </head>
      <body>
        <div id="viewDiv">
    
          <div id="measurements" class="geoscene-widget">
    
        </div>
      </div>
      </body>
    </html>
  5. 创建 switch 语句,以调用 getArea 函数或 getLength 函数,具体取决于几何是 polygon 还是 polyline

                                                                                                                                                                                                                 
    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
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
        <title>Get length and area</title>
        <style>
          html,
          body,
          #viewDiv {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
          #measurements {
            padding: 4px 8px;
            font-size: 16px;
            bottom: 15px;
            left: 50%;
            margin-right: -50%;
            transform: translate(-50%,-50%);
        </style>
    
      <link rel="stylesheet" href="https://js.geoscene.cn/4.23/geoscene/themes/light/main.css">
      <script src="https://js.geoscene.cn/4.23/"></script>
        <script>
          require([
            "geoscene/config",
            "geoscene/Map",
            "geoscene/views/MapView",
            "geoscene/widgets/ScaleBar",
            "geoscene/widgets/Sketch",
            "geoscene/Graphic",
            "geoscene/layers/GraphicsLayer",
            "geoscene/geometry/geometryEngine",
          ], (
            Map,
          ) => {
            const map = new Map({
              basemap: "tianditu-vector", // Basemap layer service
            const view = new MapView({
              container: "viewDiv",
              map: map,
              center: [-10, 30],
              zoom: 3,
            const scalebar = new ScaleBar({
              view: view,
              unit: "metric"
            view.ui.add(scalebar, "bottom-right");
            const graphicsLayer = new GraphicsLayer();
            const sketch = new Sketch({
              layer: graphicsLayer,
              view: view,
              availableCreateTools: ["polyline", "polygon", "rectangle"],
              creationMode: "update",
              updateOnGraphicClick: true,
              visibleElements: {
                createTools: {
                  point: false,
                  circle: false
                selectionTools:{
                  "lasso-selection": false,
                  "rectangle-selection":false,
                settingsMenu: false,
                undoRedoMenu: false
            view.ui.add(sketch, "top-right");
            const measurements = document.getElementById("measurements");
            view.ui.add(measurements, "manual");
            const polygon = {
                type: "polygon",
                spatialReference: {
                  wkid: 3857,
                rings: [
                    [-4508069.082189632, 3599936.936171892],
                    [-4508069.082189632, 5478453.343307884],
                    [-2629552.6750536393, 5478453.343307884],
                    [-2629552.6750536393, 3599936.936171892],
                    [-4508069.082189632, 3599936.936171892],
              const simplePolygonSymbol = {
                type: "simple-fill",
                outline: {
                  color: [200, 0, 0],
                  width: 2,
              const polygonGraphic = new Graphic({
                geometry: polygon,
                symbol: simplePolygonSymbol
            view.when(() => {
            sketch.on("update", (e) => {
              const geometry = e.graphics[0].geometry;
              if (e.state === "start") {
              if (e.state === "complete") {
                graphicsLayer.remove(graphicsLayer.graphics.getItemAt(0));
                measurements.innerHTML = null;
              if (
                (e.toolEventInfo.type === "scale-stop" ||
                  e.toolEventInfo.type === "reshape-stop" ||
                  e.toolEventInfo.type === "move-stop")
            function getArea(polygon) {
              const geodesicArea = geometryEngine.geodesicArea(polygon, "square-kilometers");
              const planarArea = geometryEngine.planarArea(polygon, "square-kilometers");
              "<b>Geodesic area</b>:  " + geodesicArea.toFixed(2) + " km\xB2" + " |   <b>Planar area</b>: " + planarArea.toFixed(2) + "  km\xB2";
            function getLength(line) {
              const geodesicLength = geometryEngine.geodesicLength(line, "kilometers");
              const planarLength = geometryEngine.planarLength(line, "kilometers");
    
              measurements.innerHTML =
                "<b>Geodesic length</b>:  " + geodesicLength.toFixed(2) + " km" + " |   <b>Planar length</b>: " + planarLength.toFixed(2) + "  km";
    
            }
    
            function switchType(geom) {
              switch (geom.type) {
                case "polygon":
                  getArea(geom);
                  break;
                case "polyline":
                  getLength(geom);
                  break;
                default:
                  console.log("No value found");
              }
            }
        </script>
      </head>
      <body>
        <div id="viewDiv">
    
          <div id="measurements" class="geoscene-widget">
    
        </div>
      </div>
      </body>
    </html>

创建默认图形以测量

要向用户展示与应用程序交互的方式,请创建一个默认多边形,并在应用程序启动时显示其区域。

  1. 创建 polygon。将 type 设置为 polygon,并将 spatialReference 属性中的 wkid 设置为 3857 (Web Mercator)。创建 simplePolygonSymbol 来设置图形样式。

                                                                                                                                                                                                                 
    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
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
        <title>Get length and area</title>
        <style>
          html,
          body,
          #viewDiv {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
          #measurements {
            padding: 4px 8px;
            font-size: 16px;
            bottom: 15px;
            left: 50%;
            margin-right: -50%;
            transform: translate(-50%,-50%);
        </style>
    
      <link rel="stylesheet" href="https://js.geoscene.cn/4.23/geoscene/themes/light/main.css">
      <script src="https://js.geoscene.cn/4.23/"></script>
        <script>
          require([
            "geoscene/config",
            "geoscene/Map",
            "geoscene/views/MapView",
            "geoscene/widgets/ScaleBar",
            "geoscene/widgets/Sketch",
            "geoscene/Graphic",
            "geoscene/layers/GraphicsLayer",
            "geoscene/geometry/geometryEngine",
          ], (
            Map,
          ) => {
            const map = new Map({
              basemap: "tianditu-vector", // Basemap layer service
            const view = new MapView({
              container: "viewDiv",
              map: map,
              center: [-10, 30],
              zoom: 3,
            const scalebar = new ScaleBar({
              view: view,
              unit: "metric"
            view.ui.add(scalebar, "bottom-right");
            const graphicsLayer = new GraphicsLayer();
            const sketch = new Sketch({
              layer: graphicsLayer,
              view: view,
              availableCreateTools: ["polyline", "polygon", "rectangle"],
              creationMode: "update",
              updateOnGraphicClick: true,
              visibleElements: {
                createTools: {
                  point: false,
                  circle: false
                selectionTools:{
                  "lasso-selection": false,
                  "rectangle-selection":false,
                settingsMenu: false,
                undoRedoMenu: false
            view.ui.add(sketch, "top-right");
            const measurements = document.getElementById("measurements");
            view.ui.add(measurements, "manual");
    
            const polygon = {
                type: "polygon",
                spatialReference: {
                  wkid: 3857,
                },
                rings: [
                  [
                    [-4508069.082189632, 3599936.936171892],
                    [-4508069.082189632, 5478453.343307884],
                    [-2629552.6750536393, 5478453.343307884],
                    [-2629552.6750536393, 3599936.936171892],
                    [-4508069.082189632, 3599936.936171892],
                  ],
                ],
              };
    
              const simplePolygonSymbol = {
                type: "simple-fill",
                outline: {
                  color: [200, 0, 0],
                  width: 2,
                },
              };
    
              const polygonGraphic = new Graphic({
                geometry: polygon,
                symbol: simplePolygonSymbol
            view.when(() => {
            sketch.on("update", (e) => {
              const geometry = e.graphics[0].geometry;
              if (e.state === "start") {
              if (e.state === "complete") {
                graphicsLayer.remove(graphicsLayer.graphics.getItemAt(0));
                measurements.innerHTML = null;
              if (
                (e.toolEventInfo.type === "scale-stop" ||
                  e.toolEventInfo.type === "reshape-stop" ||
                  e.toolEventInfo.type === "move-stop")
            function getArea(polygon) {
              const geodesicArea = geometryEngine.geodesicArea(polygon, "square-kilometers");
              const planarArea = geometryEngine.planarArea(polygon, "square-kilometers");
              "<b>Geodesic area</b>:  " + geodesicArea.toFixed(2) + " km\xB2" + " |   <b>Planar area</b>: " + planarArea.toFixed(2) + "  km\xB2";
            function getLength(line) {
              const geodesicLength = geometryEngine.geodesicLength(line, "kilometers");
              const planarLength = geometryEngine.planarLength(line, "kilometers");
                "<b>Geodesic length</b>:  " + geodesicLength.toFixed(2) + " km" + " |   <b>Planar length</b>: " + planarLength.toFixed(2) + "  km";
            function switchType(geom) {
              switch (geom.type) {
                case "polygon":
                  break;
                case "polyline":
                  break;
                default:
                  console.log("No value found");
        </script>
      </head>
      <body>
        <div id="viewDiv">
    
          <div id="measurements" class="geoscene-widget">
    
        </div>
      </div>
      </body>
    </html>
  2. 创建图形类实例。将 geometrysymbol 属性设置为 polygonsimplePolygonSymbol。将图形添加到 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
    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
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
        <title>Get length and area</title>
        <style>
          html,
          body,
          #viewDiv {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
          #measurements {
            padding: 4px 8px;
            font-size: 16px;
            bottom: 15px;
            left: 50%;
            margin-right: -50%;
            transform: translate(-50%,-50%);
        </style>
    
      <link rel="stylesheet" href="https://js.geoscene.cn/4.23/geoscene/themes/light/main.css">
      <script src="https://js.geoscene.cn/4.23/"></script>
        <script>
          require([
            "geoscene/config",
            "geoscene/Map",
            "geoscene/views/MapView",
            "geoscene/widgets/ScaleBar",
            "geoscene/widgets/Sketch",
            "geoscene/Graphic",
            "geoscene/layers/GraphicsLayer",
            "geoscene/geometry/geometryEngine",
          ], (
            Map,
          ) => {
            const map = new Map({
              basemap: "tianditu-vector", // Basemap layer service
            const view = new MapView({
              container: "viewDiv",
              map: map,
              center: [-10, 30],
              zoom: 3,
            const scalebar = new ScaleBar({
              view: view,
              unit: "metric"
            view.ui.add(scalebar, "bottom-right");
            const graphicsLayer = new GraphicsLayer();
            const sketch = new Sketch({
              layer: graphicsLayer,
              view: view,
              availableCreateTools: ["polyline", "polygon", "rectangle"],
              creationMode: "update",
              updateOnGraphicClick: true,
              visibleElements: {
                createTools: {
                  point: false,
                  circle: false
                selectionTools:{
                  "lasso-selection": false,
                  "rectangle-selection":false,
                settingsMenu: false,
                undoRedoMenu: false
            view.ui.add(sketch, "top-right");
            const measurements = document.getElementById("measurements");
            view.ui.add(measurements, "manual");
    
            const polygon = {
                type: "polygon",
                spatialReference: {
                  wkid: 3857,
                },
                rings: [
                  [
                    [-4508069.082189632, 3599936.936171892],
                    [-4508069.082189632, 5478453.343307884],
                    [-2629552.6750536393, 5478453.343307884],
                    [-2629552.6750536393, 3599936.936171892],
                    [-4508069.082189632, 3599936.936171892],
                  ],
                ],
              };
    
              const simplePolygonSymbol = {
                type: "simple-fill",
                outline: {
                  color: [200, 0, 0],
                  width: 2,
                },
              };
    
              const polygonGraphic = new Graphic({
                geometry: polygon,
                symbol: simplePolygonSymbol
              });
    
            graphicsLayer.add(polygonGraphic);
            view.when(() => {
            sketch.on("update", (e) => {
              const geometry = e.graphics[0].geometry;
              if (e.state === "start") {
              if (e.state === "complete") {
                graphicsLayer.remove(graphicsLayer.graphics.getItemAt(0));
                measurements.innerHTML = null;
              if (
                (e.toolEventInfo.type === "scale-stop" ||
                  e.toolEventInfo.type === "reshape-stop" ||
                  e.toolEventInfo.type === "move-stop")
            function getArea(polygon) {
              const geodesicArea = geometryEngine.geodesicArea(polygon, "square-kilometers");
              const planarArea = geometryEngine.planarArea(polygon, "square-kilometers");
              "<b>Geodesic area</b>:  " + geodesicArea.toFixed(2) + " km\xB2" + " |   <b>Planar area</b>: " + planarArea.toFixed(2) + "  km\xB2";
            function getLength(line) {
              const geodesicLength = geometryEngine.geodesicLength(line, "kilometers");
              const planarLength = geometryEngine.planarLength(line, "kilometers");
                "<b>Geodesic length</b>:  " + geodesicLength.toFixed(2) + " km" + " |   <b>Planar length</b>: " + planarLength.toFixed(2) + "  km";
            function switchType(geom) {
              switch (geom.type) {
                case "polygon":
                  break;
                case "polyline":
                  break;
                default:
                  console.log("No value found");
        </script>
      </head>
      <body>
        <div id="viewDiv">
    
          <div id="measurements" class="geoscene-widget">
    
        </div>
      </div>
      </body>
    </html>
  3. when 加载 view 时,调用 polygonGraphic 上的 update 方法,并基于 polygonGraphicgeometry 调用 getArea 函数。

                                                                                                                                                                                                                 
    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
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
        <title>Get length and area</title>
        <style>
          html,
          body,
          #viewDiv {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
          #measurements {
            padding: 4px 8px;
            font-size: 16px;
            bottom: 15px;
            left: 50%;
            margin-right: -50%;
            transform: translate(-50%,-50%);
        </style>
    
      <link rel="stylesheet" href="https://js.geoscene.cn/4.23/geoscene/themes/light/main.css">
      <script src="https://js.geoscene.cn/4.23/"></script>
        <script>
          require([
            "geoscene/config",
            "geoscene/Map",
            "geoscene/views/MapView",
            "geoscene/widgets/ScaleBar",
            "geoscene/widgets/Sketch",
            "geoscene/Graphic",
            "geoscene/layers/GraphicsLayer",
            "geoscene/geometry/geometryEngine",
          ], (
            Map,
          ) => {
            const map = new Map({
              basemap: "tianditu-vector", // Basemap layer service
            const view = new MapView({
              container: "viewDiv",
              map: map,
              center: [-10, 30],
              zoom: 3,
            const scalebar = new ScaleBar({
              view: view,
              unit: "metric"
            view.ui.add(scalebar, "bottom-right");
            const graphicsLayer = new GraphicsLayer();
            const sketch = new Sketch({
              layer: graphicsLayer,
              view: view,
              availableCreateTools: ["polyline", "polygon", "rectangle"],
              creationMode: "update",
              updateOnGraphicClick: true,
              visibleElements: {
                createTools: {
                  point: false,
                  circle: false
                selectionTools:{
                  "lasso-selection": false,
                  "rectangle-selection":false,
                settingsMenu: false,
                undoRedoMenu: false
            view.ui.add(sketch, "top-right");
            const measurements = document.getElementById("measurements");
            view.ui.add(measurements, "manual");
            const polygon = {
                type: "polygon",
                spatialReference: {
                  wkid: 3857,
                rings: [
                    [-4508069.082189632, 3599936.936171892],
                    [-4508069.082189632, 5478453.343307884],
                    [-2629552.6750536393, 5478453.343307884],
                    [-2629552.6750536393, 3599936.936171892],
                    [-4508069.082189632, 3599936.936171892],
              const simplePolygonSymbol = {
                type: "simple-fill",
                outline: {
                  color: [200, 0, 0],
                  width: 2,
              const polygonGraphic = new Graphic({
                geometry: polygon,
                symbol: simplePolygonSymbol
            graphicsLayer.add(polygonGraphic);
    
            view.when(() => {
              sketch.update(polygonGraphic);
              getArea(polygonGraphic.geometry);
            });
            sketch.on("update", (e) => {
              const geometry = e.graphics[0].geometry;
              if (e.state === "start") {
              if (e.state === "complete") {
                graphicsLayer.remove(graphicsLayer.graphics.getItemAt(0));
                measurements.innerHTML = null;
              if (
                (e.toolEventInfo.type === "scale-stop" ||
                  e.toolEventInfo.type === "reshape-stop" ||
                  e.toolEventInfo.type === "move-stop")
            function getArea(polygon) {
              const geodesicArea = geometryEngine.geodesicArea(polygon, "square-kilometers");
              const planarArea = geometryEngine.planarArea(polygon, "square-kilometers");
              "<b>Geodesic area</b>:  " + geodesicArea.toFixed(2) + " km\xB2" + " |   <b>Planar area</b>: " + planarArea.toFixed(2) + "  km\xB2";
            function getLength(line) {
              const geodesicLength = geometryEngine.geodesicLength(line, "kilometers");
              const planarLength = geometryEngine.planarLength(line, "kilometers");
                "<b>Geodesic length</b>:  " + geodesicLength.toFixed(2) + " km" + " |   <b>Planar length</b>: " + planarLength.toFixed(2) + "  km";
            function switchType(geom) {
              switch (geom.type) {
                case "polygon":
                  break;
                case "polyline":
                  break;
                default:
                  console.log("No value found");
        </script>
      </head>
      <body>
        <div id="viewDiv">
    
          <div id="measurements" class="geoscene-widget">
    
        </div>
      </div>
      </body>
    </html>

添加事件监听器

Sketch 微件的设置允许您编辑在视图上绘制的几何。创建事件监听器以在更新时注册几何的不同状态,并动态测量其面积或长度。

  1. 创建一个事件监听器,用于在创建、调整大小或移动图形时监听 update on 的更改。使用事件中第一个图形的 geometry 设置 geometry 元素。

                                                                                                                                                                                                                 
    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
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
        <title>Get length and area</title>
        <style>
          html,
          body,
          #viewDiv {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
          #measurements {
            padding: 4px 8px;
            font-size: 16px;
            bottom: 15px;
            left: 50%;
            margin-right: -50%;
            transform: translate(-50%,-50%);
        </style>
    
      <link rel="stylesheet" href="https://js.geoscene.cn/4.23/geoscene/themes/light/main.css">
      <script src="https://js.geoscene.cn/4.23/"></script>
        <script>
          require([
            "geoscene/config",
            "geoscene/Map",
            "geoscene/views/MapView",
            "geoscene/widgets/ScaleBar",
            "geoscene/widgets/Sketch",
            "geoscene/Graphic",
            "geoscene/layers/GraphicsLayer",
            "geoscene/geometry/geometryEngine",
          ], (
            Map,
          ) => {
            const map = new Map({
              basemap: "tianditu-vector", // Basemap layer service
            const view = new MapView({
              container: "viewDiv",
              map: map,
              center: [-10, 30],
              zoom: 3,
            const scalebar = new ScaleBar({
              view: view,
              unit: "metric"
            view.ui.add(scalebar, "bottom-right");
            const graphicsLayer = new GraphicsLayer();
            const sketch = new Sketch({
              layer: graphicsLayer,
              view: view,
              availableCreateTools: ["polyline", "polygon", "rectangle"],
              creationMode: "update",
              updateOnGraphicClick: true,
              visibleElements: {
                createTools: {
                  point: false,
                  circle: false
                selectionTools:{
                  "lasso-selection": false,
                  "rectangle-selection":false,
                settingsMenu: false,
                undoRedoMenu: false
            view.ui.add(sketch, "top-right");
            const measurements = document.getElementById("measurements");
            view.ui.add(measurements, "manual");
            const polygon = {
                type: "polygon",
                spatialReference: {
                  wkid: 3857,
                rings: [
                    [-4508069.082189632, 3599936.936171892],
                    [-4508069.082189632, 5478453.343307884],
                    [-2629552.6750536393, 5478453.343307884],
                    [-2629552.6750536393, 3599936.936171892],
                    [-4508069.082189632, 3599936.936171892],
              const simplePolygonSymbol = {
                type: "simple-fill",
                outline: {
                  color: [200, 0, 0],
                  width: 2,
              const polygonGraphic = new Graphic({
                geometry: polygon,
                symbol: simplePolygonSymbol
            view.when(() => {
              sketch.update(polygonGraphic);
              getArea(polygonGraphic.geometry);
            });
    
            sketch.on("update", (e) => {
              const geometry = e.graphics[0].geometry;
    
              if (e.state === "start") {
              if (e.state === "complete") {
                graphicsLayer.remove(graphicsLayer.graphics.getItemAt(0));
                measurements.innerHTML = null;
              if (
                (e.toolEventInfo.type === "scale-stop" ||
                  e.toolEventInfo.type === "reshape-stop" ||
                  e.toolEventInfo.type === "move-stop")
            });
            function getArea(polygon) {
              const geodesicArea = geometryEngine.geodesicArea(polygon, "square-kilometers");
              const planarArea = geometryEngine.planarArea(polygon, "square-kilometers");
              "<b>Geodesic area</b>:  " + geodesicArea.toFixed(2) + " km\xB2" + " |   <b>Planar area</b>: " + planarArea.toFixed(2) + "  km\xB2";
            function getLength(line) {
              const geodesicLength = geometryEngine.geodesicLength(line, "kilometers");
              const planarLength = geometryEngine.planarLength(line, "kilometers");
                "<b>Geodesic length</b>:  " + geodesicLength.toFixed(2) + " km" + " |   <b>Planar length</b>: " + planarLength.toFixed(2) + "  km";
            function switchType(geom) {
              switch (geom.type) {
                case "polygon":
                  break;
                case "polyline":
                  break;
                default:
                  console.log("No value found");
        </script>
      </head>
      <body>
        <div id="viewDiv">
    
          <div id="measurements" class="geoscene-widget">
    
        </div>
      </div>
      </body>
    </html>
  2. 根据 sketch 事件的 statestartcomplete,还是处于更改状态而创建条件语句。对于每个状态 (complete 除外),调用 switchType 语句,其中 geometry 为其参数。当事件 complete 后,从 graphicsLayer 中移除图形并清除 innerHTML

                                                                                                                                                                                                                 
    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
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
        <title>Get length and area</title>
        <style>
          html,
          body,
          #viewDiv {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
          #measurements {
            padding: 4px 8px;
            font-size: 16px;
            bottom: 15px;
            left: 50%;
            margin-right: -50%;
            transform: translate(-50%,-50%);
        </style>
    
      <link rel="stylesheet" href="https://js.geoscene.cn/4.23/geoscene/themes/light/main.css">
      <script src="https://js.geoscene.cn/4.23/"></script>
        <script>
          require([
            "geoscene/config",
            "geoscene/Map",
            "geoscene/views/MapView",
            "geoscene/widgets/ScaleBar",
            "geoscene/widgets/Sketch",
            "geoscene/Graphic",
            "geoscene/layers/GraphicsLayer",
            "geoscene/geometry/geometryEngine",
          ], (
            Map,
          ) => {
            const map = new Map({
              basemap: "tianditu-vector", // Basemap layer service
            const view = new MapView({
              container: "viewDiv",
              map: map,
              center: [-10, 30],
              zoom: 3,
            const scalebar = new ScaleBar({
              view: view,
              unit: "metric"
            view.ui.add(scalebar, "bottom-right");
            const graphicsLayer = new GraphicsLayer();
            const sketch = new Sketch({
              layer: graphicsLayer,
              view: view,
              availableCreateTools: ["polyline", "polygon", "rectangle"],
              creationMode: "update",
              updateOnGraphicClick: true,
              visibleElements: {
                createTools: {
                  point: false,
                  circle: false
                selectionTools:{
                  "lasso-selection": false,
                  "rectangle-selection":false,
                settingsMenu: false,
                undoRedoMenu: false
            view.ui.add(sketch, "top-right");
            const measurements = document.getElementById("measurements");
            view.ui.add(measurements, "manual");
            const polygon = {
                type: "polygon",
                spatialReference: {
                  wkid: 3857,
                rings: [
                    [-4508069.082189632, 3599936.936171892],
                    [-4508069.082189632, 5478453.343307884],
                    [-2629552.6750536393, 5478453.343307884],
                    [-2629552.6750536393, 3599936.936171892],
                    [-4508069.082189632, 3599936.936171892],
              const simplePolygonSymbol = {
                type: "simple-fill",
                outline: {
                  color: [200, 0, 0],
                  width: 2,
              const polygonGraphic = new Graphic({
                geometry: polygon,
                symbol: simplePolygonSymbol
            view.when(() => {
            sketch.on("update", (e) => {
              const geometry = e.graphics[0].geometry;
    
              if (e.state === "start") {
                switchType(geometry);
              }
    
              if (e.state === "complete") {
                graphicsLayer.remove(graphicsLayer.graphics.getItemAt(0));
                measurements.innerHTML = null;
              }
    
              if (
                e.toolEventInfo &&
                (e.toolEventInfo.type === "scale-stop" ||
                  e.toolEventInfo.type === "reshape-stop" ||
                  e.toolEventInfo.type === "move-stop")
              ) {
                switchType(geometry);
              }
    
            });
            function getArea(polygon) {
              const geodesicArea = geometryEngine.geodesicArea(polygon, "square-kilometers");
              const planarArea = geometryEngine.planarArea(polygon, "square-kilometers");
              "<b>Geodesic area</b>:  " + geodesicArea.toFixed(2) + " km\xB2" + " |   <b>Planar area</b>: " + planarArea.toFixed(2) + "  km\xB2";
            function getLength(line) {
              const geodesicLength = geometryEngine.geodesicLength(line, "kilometers");
              const planarLength = geometryEngine.planarLength(line, "kilometers");
                "<b>Geodesic length</b>:  " + geodesicLength.toFixed(2) + " km" + " |   <b>Planar length</b>: " + planarLength.toFixed(2) + "  km";
            function switchType(geom) {
              switch (geom.type) {
                case "polygon":
                  break;
                case "polyline":
                  break;
                default:
                  console.log("No value found");
        </script>
      </head>
      <body>
        <div id="viewDiv">
    
          <div id="measurements" class="geoscene-widget">
    
        </div>
      </div>
      </body>
    </html>

运行应用程序

CodePen 中,运行代码以显示地图。

运行应用程序时,您将看到一个面及其计算出的测地线面积和平面面积。您可以使用草绘微件绘制其他几何并显示其测量值。如果移动几何,测地线测量值将更改,但平面测量值不会更改。

下一步是什么?

要了解如何使用其他API 功能,请参阅以下教程:

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