首页 > 其他分享 >ArcGIS API for JS4.8绘制点、线、面、矩形、圆

ArcGIS API for JS4.8绘制点、线、面、矩形、圆

时间:2023-09-14 18:01:48浏览次数:46  
标签:function draw var ArcGIS API JS4.8 绘制 action view

实现代码

使用ArcGIS API for JS4.8绘制点(Point)、线(Polyline)、面(Polygon)、矩形(Rectangle)、圆(Circle),使用Draw绘制,具体代码如下:

<!DOCTYPE html>
<html>
 
<head>
    <meta charset="utf-8"/>
    <title>ArcGIS demo</title>
    <link type="text/css" rel="stylesheet" href="http://localhost/arcgis_js_api/library/4.8/esri/css/main.css"/>
    <script src="http://localhost/arcgis_js_api/library/4.8/init.js"></script>
 
    <style>
        html,
        body,
        #viewDiv {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
        }
    </style>
</head>
 
<body>
<div id="viewDiv">
    <div id="line-button" class="esri-widget esri-widget--button esri-interactive" title="画线">
        <span class="esri-icon-polyline"></span>
    </div>
    <div id="area-button" class="esri-widget esri-widget--button esri-interactive" title="画面">
        <span class="esri-icon-polygon"></span>
    </div>
    <div id="point-button" class="esri-widget esri-widget--button esri-interactive" title="画点">
        <span class="esri-icon-radio-checked"></span>
    </div>
    <div id="circle-button" class="esri-widget esri-widget--button esri-interactive" title="画圆">
        <span class="esri-icon-radio-unchecked"></span>
    </div>
    <div id="rectangle-button" class="esri-widget esri-widget--button esri-interactive" title="画矩形">
        <span class="esri-icon-checkbox-unchecked"></span>
    </div>
</div>
<script>
    require([
        "esri/Map",
        "esri/views/MapView",
        "esri/views/2d/draw/Draw",
        "esri/Graphic",
        "esri/geometry/Polyline",
        "esri/geometry/Polygon",
        "esri/geometry/Point",
        "esri/geometry/Circle",
        "dojo/domReady!"
    ], function (
                 Map, MapView,
                 Draw, Graphic,
                 Polyline, Polygon,Point,Circle
                 ) {
        var map = new Map({
            basemap: "dark-gray"
        });
 
        //二维视图
        var view = new MapView({
            map: map,
            container: "viewDiv"
        });
        view.ui.add("line-button", "top-left");//添加绘制线按钮,自定义UI
        view.ui.add("area-button", "top-left");//添加绘制面按钮,自定义UI
        view.ui.add("point-button", "top-left");//添加绘制面按钮,自定义UI
        view.ui.add("circle-button", "top-left");//添加绘制面按钮,自定义UI
        view.ui.add("rectangle-button", "top-left");//添加绘制面按钮,自定义UI
        // view.ui.remove("attribution");//移除底部ESRI logo
 
        view.when(function () {
            var draw = new Draw({
                view: view
            });
            //绑定线按钮绘制事件
            var drawLineButton = document.getElementById("line-button");
            drawLineButton.onclick = function() {
                view.graphics.removeAll();//清楚之前的绘制
                enableCreateLine(draw, view);
            };
            //绑定面按钮绘制事件
            var drawAreaButton = document.getElementById("area-button");
            drawAreaButton.onclick = function() {
                view.graphics.removeAll();//清楚之前的绘制
                enableCreateArea(draw, view);
            };
            //绑定面按钮绘制事件
            var drawPointButton = document.getElementById("point-button");
            drawPointButton.onclick = function() {
                enableCreatePoint(draw, view);
            };
            //绑定面按钮绘制事件
            var drawCircleButton = document.getElementById("circle-button");
            drawCircleButton.onclick = function() {
                enableCreateCircle(draw, view);
            };
            //绑定面按钮绘制事件
            var drawRectangleButton = document.getElementById("rectangle-button");
            drawRectangleButton.onclick = function() {
                enableCreateRectangle(draw, view);
            };
        });
        //开始监听画线
        function enableCreateLine(draw, view) {
            var action = draw.create("polyline", {
                mode: "click"
            });
            // 获取焦点
            view.focus();
 
            // 顶点添加事件
            action.on("vertex-add", createPolyline);
 
 
            //顶点移除事件
            action.on("vertex-remove", createPolyline);
 
 
            // 鼠标移动事件
            action.on("cursor-update", createPolyline);
 
 
            // 绘制完成事件
            action.on("draw-complete", createPolyline);
 
 
        }
        //开始监听画面
        function enableCreateArea(draw, view) {
            var action = draw.create("polygon", {
                mode: "click"//点击方式加点
            });
            // 获取焦点
            view.focus();
 
            // 顶点添加事件
            action.on("vertex-add", createPolygon);
 
 
            //顶点移除事件
            action.on("vertex-remove", createPolygon);
 
 
            // 鼠标移动事件
            action.on("cursor-update", createPolygon);
 
 
            // 绘制完成事件
            action.on("draw-complete", createPolygon);
 
 
        }
        //开始监听画点
        function enableCreatePoint(draw, view) {
            var action = draw.create("point", {
                mode: "click"//点击方式加点
            });
            // 获取焦点
            view.focus();
 
            // 顶点添加事件
            action.on("vertex-add", createPoint);
 
 
            //顶点移除事件
            action.on("vertex-remove", createPoint);
 
 
            // 绘制完成事件
            action.on("draw-complete", createPoint);
 
 
        }
        //开始监听画圆
        function enableCreateCircle(draw, view) {
            var action = draw.create("circle", {
                mode: "click"//点击方式加点
            });
            // 获取焦点
            view.focus();
            //顶点移除事件
            action.on("vertex-remove", createCircle);
            // 鼠标移动事件
            action.on("cursor-update", createCircle);
            // 绘制完成事件
            action.on("draw-complete", createCircle);
        }
        //开始监听画矩形
        function enableCreateRectangle(draw, view) {
            var action = draw.create("rectangle", {
                mode: "click"//点击方式加点
            });
            // 获取焦点
            view.focus();
 
            //顶点移除事件
            action.on("vertex-remove", createRectangle);
            // 鼠标移动事件
            action.on("cursor-update", createRectangle);
            // 绘制完成事件
            action.on("draw-complete", createRectangle);
 
        }
        //根据点坐标生成新的线
        function createPolyline(event) {
            //获取所有顶点
            var vertices = event.vertices;
            //清除之前绘制
            view.graphics.removeAll();
            // 生成绘制的图形
            var graphic = new Graphic({
                geometry: new Polyline({
                    paths: vertices,
                    spatialReference: view.spatialReference
                }),
                symbol: {
                    type: "simple-line", // autocasts as new SimpleFillSymbol
                    color: [4, 90, 141],
                    width: 4,
                    cap: "round",
                    join: "round"
                }
            });
           // 将绘制的图形添加到view
            view.graphics.add(graphic);
        };
        //根据点坐标生成新的线
        function createPolygon(event) {
            //获取所有顶点
            var vertices = event.vertices;
            //清除之前绘制
            view.graphics.removeAll();
 
            // 生成绘制的图形
            var graphic = new Graphic({
                geometry: new Polygon({
                    hasZ: false,
                    hasM: false,
                    rings: [vertices],
                    spatialReference: view.spatialReference
                }),
                symbol: {
                    type: "simple-fill",  // autocasts as new SimpleFillSymbol()
                    color: [ 51,51, 204, 0.9 ],
                    style: "solid",
                    outline: {  // autocasts as new SimpleLineSymbol()
                        color: "white",
                        width: 1
                    }
                }
            });
            // 将绘制的图形添加到view
            view.graphics.add(graphic);
        }
 
        //根据点坐标生成新的线
        function createPoint(event) {
            //获取所有顶点
            var coordinates = event.coordinates;
 
            //生成绘制的图形
            var graphic = new Graphic({
                geometry: new Point({
                    hasZ: false,
                    hasM: false,
                    x:coordinates[0],
                    y:coordinates[1],
                    spatialReference: view.spatialReference
                }),
                symbol: {
                    type: "simple-marker",  // autocasts as new SimpleMarkerSymbol()
                    style: "square",
                    color: "blue",
                    size: "8px",  // pixels
                    outline: {  // autocasts as new SimpleLineSymbol()
                        color: [ 255, 255, 0 ],
                        width: 3  // points
                    }
                }
            });
            // 将绘制的图形添加到view
            view.graphics.add(graphic);
        }
        //根据点坐标生成新的线
        function createCircle(event) {
            //获取所有顶点
            var vertices = event.vertices;
            //少于一个点无法展示圆
            if(vertices.length<2){
                return
            }
            //清除之前绘制
            view.graphics.removeAll();
            //生成绘制的图形,两点画圆
            var center=new Point({
                hasZ: false,
                hasM: false,
                x:vertices[0][0],
                y:vertices[0][1],
                spatialReference: view.spatialReference
            });
            var dis=center.distance(new Point({
                hasZ: false,
                hasM: false,
                x:vertices[1][0],
                y:vertices[1][1],
                spatialReference: view.spatialReference
            }));
            var graphic = new Graphic({
                geometry: new Circle({
                    hasZ: false,
                    hasM: false,
                    center:center,
                    radius:dis,
                    spatialReference: view.spatialReference
                }),
                symbol: {
                    type: "simple-fill",  // autocasts as new SimpleFillSymbol()
                    color: [ 51,51, 204, 0.9 ],
                    style: "solid",
                    outline: {  // autocasts as new SimpleLineSymbol()
                        color: "white",
                        width: 1
                    }
                }
            });
            // 将绘制的图形添加到view
            view.graphics.add(graphic);
        }
        function createRectangle(event) {
            //获取所有顶点
            var vertices = event.vertices;
 
            //两点画矩形
            if(vertices.length<2){
                return
            }
            var rings=[vertices[0],[vertices[0][0],vertices[1][1]],vertices[1],[vertices[1][0],vertices[0][1]]];
            //清除之前绘制
            view.graphics.removeAll();
 
            // 生成绘制的图形
            var graphic = new Graphic({
                geometry: new Polygon({
                    hasZ: false,
                    hasM: false,
                    rings: [rings],
                    spatialReference: view.spatialReference
                }),
                symbol: {
                    type: "simple-fill",  // autocasts as new SimpleFillSymbol()
                    color: [ 51,51, 204, 0.9 ],
                    style: "solid",
                    outline: {  // autocasts as new SimpleLineSymbol()
                        color: "white",
                        width: 1
                    }
                }
            });
            // 将绘制的图形添加到view
            view.graphics.add(graphic);
        }
    })
</script>
</body>
 
</html>

实现效果





标签:function,draw,var,ArcGIS,API,JS4.8,绘制,action,view
From: https://www.cnblogs.com/ZerlinM/p/17703089.html

相关文章

  • ArcGIS10.8 安装
    一、下载ArcGIS10.8下载链接:ttps://pan.baidu.com/s/1TnTv0p9mYQVwlGMpfZb15A 提取码:kjgh下载后如下所示:解压: 二、安装安装视频教程:https://www.bilibili.com/video/BV18a411t75F/?spm_id_from=333.788.recommend_more_video.9&vd_source=f13a40031429e4d0c1a36af1......
  • .NET Core创建API项目
    新建项目类型:ASP.NETCoreWebAPIWebAPI控制器通常应派生自ControllerBase而不是Controller。Controller派生自ControllerBase,并添加对视图的支持,因此它用于处理Web页面,而不是WebAPI请求。如果同一控制器必须支持视图和WebAPI,则派生自Controller。API接口返回......
  • 三节点master修改apiserver端口
    因业务需求,需要把apiserver的6443端口改成其它端口,k8s集群是二进制部署的,有三个master节点,调整方法如下1.修改apiserver启动service中的配置查看apiserver的service文件位置systemctlstatuskube-apiserver,找到/usr/lib/systemd/system/kube-apiserver.service修改--secure-p......
  • fastapi设置响应示例
    classTest(BaseModel):name:strdescription:intcreated_at:strupdated_at:strdata:dict@validator("data",pre=True)defparse_data(cls,value):returnjson.loads(value)ifvalueelse{}@validator(......
  • 一文看懂Apipost IDEA插件2.0
    大家好,Apipost最新推出IDEA插件V2版本!V2版本主要是Apipost符合更多用户的需求而推出,支持在插件中获取token、支持代码完成后在插件中进行API调试,同时也保留了1.0版本部分功能如上传选择目录功能等。V1版本还会继续保留开源,方便各位进行自创魔改。V2版本目前已上架至IDEA插件商......
  • 速记:B站拉取饭贩(直播开放平台)身份码的API
    近期blivechat更新为接入开放平台读取身份码的方式拉取弹幕了,遂速记一下抓到的B站获取这个身份码的API。就这东西https://api.live.bilibili.com/xlive/open-platform/v1/common/operationOnBroadcastCode请求方式:POST认证方式:Cookie鉴权方式(推测):Cookie中bili_jct的值正确......
  • FastAPI学习-12. 请求Cookie 参数
    前言你可以像定义 Query 参数和 Path 参数一样来定义 Cookie 参数。声明 Cookie 参数首先,导入 Cookie:fromfastapiimportCookie,FastAPI声明 Cookie 参数的结构与声明 Query 参数和 Path 参数时相同。第一个值是参数的默认值,同时也可以传递所有验证参数......
  • 微信小程序 封装请求api
    封装请求地址https.jsletbaseUrl='https://XX.XXX.com/index.php/';//自己得服务器地址export{baseUrl}结构目录封装request.jsimport{baseUrl}from'./https.js'module.exports={request:function(url,methodType,data){letful......
  • 亚马逊API接口解析,实现按关键字搜索商品
    要解析亚马逊API接口并实现按关键字搜索商品,你需要按照以下步骤进行操作:了解亚马逊开发者中心:访问亚马逊开发者中心,并了解相关的API文档、开发者指南和规定。注册开发者账号:在亚马逊开发者中心上注册一个开发者账号,并创建一个应用,获取到API权限。获取API密钥:为了使用亚马逊API接......
  • 通过这些API,开发者可以在自己的应用程序中嵌入电商功能,为用户提供便捷的购物体验
    在当今的数字化时代,电子商务已经成为人们日常生活的重要组成部分。随着电子商务的飞速发展,许多电商平台都提供了API(应用程序接口)来允许开发者调用特定的功能,如商品查询、购物车管理、订单处理以及支付等。通过这些API,开发者可以在自己的应用程序中嵌入电商功能,为用户提供便捷的购物......