日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

arcgis api 4.x 实现动态测距和计算面积

發布時間:2023/12/29 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 arcgis api 4.x 实现动态测距和计算面积 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

先來個效果圖:
圖片是靜態的,沒有體現出來動態的效果,實際效果是測距和面積會隨著鼠標的位置實時顯示當前線段的長度或者面積大小。

本例采用arcgis api 4.x,原理是調用draw繪制工具,監聽draw過程中的不同事件,并通過事件中獲得的經緯度信息去繪制polyline和polygon。draw事件參考官方文檔:4.x draw文檔

通過"esri/geometry/geometryEngine" 中的geodesicLength()和geodesicArea()方法去計算長度和面積。官方文檔:geodesicLength()和geodesicArea()

全部代碼:復制可直接運行。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta http-equiv="X-UA-Compatible" content="IE=7,IE=9" /><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>arcgis api 4.x 實現動態測距和計算面積</title><link rel="stylesheet" href="https://js.arcgis.com/4.15/esri/themes/light/main.css"><style>body,html {margin: 0;padding: 0;width: 100%;height: 100%;font-family: Arial;}#map {width: 100%;height: 100%;margin: 0;padding: 0;border: 0px dashed black;background-color: rgb(0, 38, 48);}</style> </head> <body><button type="button" onclick="drawLine()">測距</button><button type="button" onclick="drawPolygon()">面積</button><div id="map"></div> </body> <script src="https://js.arcgis.com/4.15/"></script> <script type="text/javascript">var map, drawLine, drawPolygon;require(["esri/Map",'esri/Color','esri/symbols/SimpleLineSymbol',"esri/views/MapView","esri/layers/TileLayer",'esri/views/draw/Draw','esri/geometry/geometryEngine','esri/geometry/Point','esri/geometry/Polyline','esri/geometry/Polygon','esri/layers/GraphicsLayer','esri/Graphic',"dojo/domReady!"], function(Map,Color,SimpleLineSymbol,MapView,TileLayer,Draw,GeometryEngine,Point,Polyline,Polygon,GraphicsLayer,Graphic) {var tileLayer = new TileLayer({ //arcgis在線地圖url: "http://cache1.arcgisonline.cn/arcgis/rest/services/ChinaOnlineCommunity/MapServer"}) var lineLayer = new GraphicsLayer() //繪制線圖層var polygonLayer = new GraphicsLayer() //繪制面圖層map = new Map({layers:[tileLayer, lineLayer, polygonLayer],});map.on('load', function() {map.hideZoomSlider()map.hidePanArrows()map.disableDoubleClickZoom() //禁用雙擊縮放})var view = new MapView({//創建視圖container: "map",map: map,zoom: 8,spatialReference: {wkid: 3857},center: [110.72, 18.92] // longitude, latitude});//畫線按鈕事件drawLine = function() {let draw = new Draw({ //在view里創建drawview: view})let action = draw.create("polyline") //創建畫線實例view.focus()action.on(["vertex-add", // when a vertex is added 鼠標單擊"vertex-remove", // when a vertex is removed 移除"cursor-update", // when the pointer moves 鼠標移動"draw-complete" // when the drawing is completed 鼠標雙擊],function(evt){createLine(evt.vertices)})}//畫面按鈕事件drawPolygon = function() {let draw = new Draw({ //在view里創建drawview: view})let action = draw.create("polygon") //創建畫線實例view.focus()action.on(["vertex-add", // when a vertex is added 鼠標單擊"vertex-remove", // when a vertex is removed 移除"cursor-update", // when the pointer moves 鼠標移動"draw-complete" // when the drawing is completed 鼠標雙擊],function(evt){createPolygon(evt.vertices)})}//畫線和測距createLine = function(vertices) {lineLayer.removeAll() //清空上次繪制的線let symbol = { //點樣式type: "simple-marker",color: [47,79,79],width: 0.5,size: 4,outline: {color: [ 0, 0, 0 ],width: 1 }}//將起點添加到地圖let startGraphics = new Graphic({geometry: new Point({type: "point",x: vertices[0][0], //當底圖是投影坐標系時用x,地理坐標系用longitudey: vertices[0][1], //當底圖是投影坐標系時用y,地理坐標系用latitudespatialReference: view.spatialReference //和底圖相同的坐標系}),symbol: symbol})lineLayer.add(startGraphics)//將線添加到地圖let lineGraphics = new Graphic({geometry: new Polyline({paths: vertices,spatialReference: view.spatialReference}),symbol: { //線樣式type: "simple-line", // autocasts as new SimpleFillSymbolstyle: "dash",color: [176,196,222],width: 2}});lineLayer.add(lineGraphics)//測距let linePath = [] //線段坐標集合pointStart = [] //起點pointStart.push(vertices[0][0])pointStart.push(vertices[0][1])linePath.push(pointStart)for (let i = 1; i < vertices.length ; i++) { //獲得鼠標移動的坐標信息let point = {type:"point",x:vertices[i][0],y:vertices[i][1],spatialReference: view.spatialReference};//鼠標位置let mouseGraphics = new Graphic({geometry:point,symbol: symbol})let xy = [] //鼠標當前經緯度xy.push(vertices[i][0])xy.push(vertices[i][1]) linePath.push(xy)let line = new Polyline({ //起點到當前鼠標的線段paths: linePath,spatialReference: view.spatialReference})let length = GeometryEngine.geodesicLength(line, 'meters').toFixed(2) //測距let lengthText = lengthFormat(length) //單位轉換let textSymbol = { //距離標注type: "text",color: "white",haloColor: "black",haloSize: "2px",text: lengthText,xoffset: '50px',yoffset: '-5px',font: {size: 12,family: "sans-serif",weight: "bold"}}let textGraphics = new Graphic({ //標注位置為鼠標位置geometry:point,symbol: textSymbol});//將標注和鼠標位置添加到地圖lineLayer.addMany([textGraphics, mouseGraphics ])}}//畫面和測量面積createPolygon = function(vertices) {polygonLayer.removeAll();let symbol = { //點樣式type: "simple-marker",color: [47,79,79],width: 0.5,size: 4,outline: {color: [ 0, 0, 0 ],width: 1 }}let fillSymbol = { //面樣式type: "simple-fill", // autocasts as new SimpleFillSymbol()color: [3, 255, 240, 0.1],outline: { // autocasts as new SimpleLineSymbol()color: [255,116,3],width: 2}}let polygon = new Polygon({rings: vertices,spatialReference: view.spatialReference})let polygonGraphics = new Graphic({geometry: polygon,symbol: fillSymbol})polygonLayer.add(polygonGraphics);let area = GeometryEngine.geodesicArea(polygon,'square-meters')let areaText = areaFormat(area)let center = polygon.centroidlet pointCenter = {type:"point",longitude:center.longitude,latitude:center.latitude};let textSymbol = { //面積標注type: "text",color: "white",haloColor: "black",haloSize: "2px",text: areaText,//xoffset: '50px',//yoffset: '-5px',font: {size: 12,family: "sans-serif",weight: "bold"}}let textGraphics = new Graphic({ //標注為面中心位置geometry:pointCenter,symbol: textSymbol});polygonLayer.add(textGraphics);for (let i = 0; i <vertices.length ; i++) {let point = {type:"point",x:vertices[i][0],y:vertices[i][1],spatialReference: view.spatialReference};let pointGraphics=new Graphic({geometry:point,symbol: symbol});polygonLayer.add(pointGraphics)}}//長度單位轉換function lengthFormat(length) {if (length < 2000) {return lengthText = length + "米"}else {length = (length/1000).toFixed(2)return lengthText = length + "千米"}}//面積單位轉換function areaFormat(area) {if (area < 2000) {area = area.toFixed(2)return areaText = area + "平方米"}else {area = (area/10000).toFixed(2)return areaText = area + "平方千米"}}}); </script> </html>

總結

以上是生活随笔為你收集整理的arcgis api 4.x 实现动态测距和计算面积的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。