查找长度和面积
了解如何计算几何的长度和面积。
您可使用 geometryEngine 计算线的长度并确定面的面积。测量值取决于为几何定义的坐标系 ( 或空间参考)。如果几何的空间参考是 Web Mercator (3857) 或 WGS84 (4326),则应使用测地线计算以考虑地球的曲率。如果空间参考与 Web Mercator (3857) 或 WGS84 (4326) 不同,则应使用基于欧氏距离的平面测量。
在本教程中,您将使用草绘微件在视图上绘制图形,并使用 geometryEngine 来计算测地线和平面长度及面积,以查看两个测量值之间的差异。
步骤
创建新 Pen
设置 HTML
创建
measurements
div
以显示计算结果,并添加一些 CSS 样式来设置字体大小和边距。Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add 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", ], ( 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(); 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 }); 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") { 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"); 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"); 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>
添加模块
在
require
语句中,添加模块。GeoScene API for JavaScript 使用 AMD 模块。
require
函数用于加载模块,以便它们可在主function
中使用。保持模块引用和函数参数的顺序相同很重要。Add line. Add line. Add line. Add line. Add line. Change line Change line Change line Change line Change 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", ], ( 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(); 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 }); 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") { 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"); 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"); 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>
重定位并添加比例尺
ScaleBar 微件将在地图上显示比例尺。您可以选择公制或非公制值。例如,如果指定 metric
,则会根据比例显示公里或米。
Change line Change 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", ], ( geosceneConfig, Map, MapView, ScaleBar, Sketch, Graphic, GraphicsLayer, geometryEngine, ) => { 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 }); 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") { 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"); 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"); 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>创建
scalebar
并将view
参数设置为view
。将unit
指定为metric
。Add line. Add line. Add line. Add 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", ], ( geosceneConfig, Map, MapView, ScaleBar, Sketch, Graphic, GraphicsLayer, geometryEngine, ) => { 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 }); 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") { 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"); 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"); 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>将
scalebar
添加至view
的bottom-right
。Add 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", ], ( geosceneConfig, Map, MapView, ScaleBar, Sketch, Graphic, GraphicsLayer, geometryEngine, ) => { 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 }); 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") { 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"); 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"); 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>运行应用程序以验证
center
和zoom
级别中的更改。scalebar
应显示在视图的底部。
添加草绘微件
Sketch 微件提供的 UI 允许您在 MapView 中创建和更新图形。
创建
graphicsLayer
以将GraphicsLayer
添加到map
。Add line. Add 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", ], ( geosceneConfig, Map, MapView, ScaleBar, Sketch, Graphic, GraphicsLayer, geometryEngine, ) => { 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 }); 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") { 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"); 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"); 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>创建
sketch
微件。将layer
参数设置为graphicsLayer
。使用微件绘制的任何几何都将添加到graphicsLayer
中。通过将availableCreateTools
设置为仅显示polyline
、polygon
和rectangle
选项并禁用在visibleElements
和selectionTools
属性中找到的其他默认设置来限制微件上的要素。Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add 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", ], ( geosceneConfig, Map, MapView, ScaleBar, Sketch, Graphic, GraphicsLayer, geometryEngine, ) => { 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 }); 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") { 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"); 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"); 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>将
sketch
微件添加到view
的top-right
处。Add 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", ], ( geosceneConfig, Map, MapView, ScaleBar, Sketch, Graphic, GraphicsLayer, geometryEngine, ) => { 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 }); 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") { 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"); 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"); 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>将
measurements
元素添加到view
中以在绘制几何时显示测量值。Add line. Add 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", ], ( geosceneConfig, Map, MapView, ScaleBar, Sketch, Graphic, GraphicsLayer, geometryEngine, ) => { 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 }); 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") { 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"); 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"); 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>运行应用程序以验证微件是否显示在视图中,以及您是否能够绘制几何。
计算长度和面积
geometryEngine
允许您计算几何的平面长度/面积或测地线长度/面积。由于此应用程序中的几何使用 Web Mercator 进行投影,因此最佳做法是使用测地线测量。但是,要可视化测地线计算和平面计算之间的差异,请在绘制几何时同时测量这两者。
创建
getArea
函数,该函数使用polygon
作为其参数。调用geodesicArea()
方法和planarArea()
方法来计算多边形的面积,以square-kilometers
为单位。Add line. Add line. Add line. Add line. Add 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", ], ( geosceneConfig, Map, MapView, ScaleBar, Sketch, Graphic, GraphicsLayer, geometryEngine, ) => { 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 }); 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") { 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"); 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"); 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>将计算结果追加到
measurements
的innerHTML
。Add line. Add 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", ], ( geosceneConfig, Map, MapView, ScaleBar, Sketch, Graphic, GraphicsLayer, geometryEngine, ) => { 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 }); 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") { 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"); 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"); 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>创建
getLength
函数,该函数使用line
参数。调用geodesicLength
和planarLength
方法来计算line
的长度,以千米为单位。Add line. Add line. Add line. Add line. Add 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", ], ( geosceneConfig, Map, MapView, ScaleBar, Sketch, Graphic, GraphicsLayer, geometryEngine, ) => { 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 }); 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") { 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"); 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"); 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>将测地线长度计算的结果追加到
measurements
的innerHTML
。Add line. Add 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", ], ( geosceneConfig, Map, MapView, ScaleBar, Sketch, Graphic, GraphicsLayer, geometryEngine, ) => { 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 }); 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") { 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"); 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"); 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>创建
switch
语句,以调用getArea
函数或getLength
函数,具体取决于几何是polygon
还是polyline
。Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add 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", ], ( geosceneConfig, Map, MapView, ScaleBar, Sketch, Graphic, GraphicsLayer, geometryEngine, ) => { 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 }); 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") { 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"); 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"); 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>
创建默认图形以测量
要向用户展示与应用程序交互的方式,请创建一个默认多边形,并在应用程序启动时显示其区域。
创建
polygon
。将type
设置为polygon
,并将spatialReference
属性中的wkid
设置为3857
(Web Mercator)。创建simplePolygonSymbol
来设置图形样式。Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add 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", ], ( geosceneConfig, Map, MapView, ScaleBar, Sketch, Graphic, GraphicsLayer, geometryEngine, ) => { 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 }); 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") { 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"); 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"); 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>创建图形类实例。将
geometry
和symbol
属性设置为polygon
和simplePolygonSymbol
。将图形添加到graphicsLayer
。Add line. Add line. Add line. Add line. Add line. Add 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", ], ( geosceneConfig, Map, MapView, ScaleBar, Sketch, Graphic, GraphicsLayer, geometryEngine, ) => { 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 }); 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") { 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"); 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"); 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>when
加载view
时,调用polygonGraphic
上的update
方法,并基于polygonGraphic
的geometry
调用getArea
函数。Add line. Add line. Add line. Add 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", ], ( geosceneConfig, Map, MapView, ScaleBar, Sketch, Graphic, GraphicsLayer, geometryEngine, ) => { 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 }); 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") { 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"); 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"); 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>
添加事件监听器
Sketch 微件的设置允许您编辑在视图上绘制的几何。创建事件监听器以在更新时注册几何的不同状态,并动态测量其面积或长度。
创建一个事件监听器,用于在创建、调整大小或移动图形时监听
update
on
的更改。使用事件中第一个图形的geometry
设置geometry
元素。Add line. Add line. Add line. Add 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", ], ( geosceneConfig, Map, MapView, ScaleBar, Sketch, Graphic, GraphicsLayer, geometryEngine, ) => { 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 }); 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") { 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"); 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"); 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>根据
sketch
事件的state
是start
、complete
,还是处于更改状态而创建条件语句。对于每个状态 (complete
除外),调用switchType
语句,其中geometry
为其参数。当事件complete
后,从graphicsLayer
中移除图形并清除innerHTML
。Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add 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", ], ( geosceneConfig, Map, MapView, ScaleBar, Sketch, Graphic, GraphicsLayer, geometryEngine, ) => { 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 }); 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") { 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"); 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"); 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>
运行应用程序
在 CodePen 中,运行代码以显示地图。
运行应用程序时,您将看到一个面及其计算出的测地线面积和平面面积。您可以使用草绘微件绘制其他几何并显示其测量值。如果移动几何,测地线测量值将更改,但平面测量值不会更改。
下一步是什么?
要了解如何使用其他API 功能,请参阅以下教程: