如何在ArcGIS JavaScript API中绘制矩形框?

使用ArcGIS JavaScript API绘制矩形

arcgisjs画矩形

ArcGIS JavaScript API(简称ArcGIS API for JavaScript)是Esri公司提供的一个用于开发地理信息系统应用的JavaScript库,通过这个API,开发者可以在网页上创建交互式的地图应用,本文将详细介绍如何使用ArcGIS JavaScript API在地图上绘制一个矩形。

环境准备

1、引入ArcGIS JavaScript API:首先需要在HTML文件中引入ArcGIS JavaScript API,可以通过CDN方式引入。

    <script src="https://js.arcgis.com/4.25/"></script>

2、创建基础的HTML结构:包括一个用于显示地图的div元素,以及一个按钮来触发绘制矩形的功能。

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Draw Rectangle</title>
        <link rel="stylesheet" href="https://js.arcgis.com/4.25/esri/themes/light/main.css">
        <style>
            html, body, #viewDiv {
                padding: 0;
                margin: 0;
                height: 100%;
                width: 100%;
            }
        </style>
    </head>
    <body>
        <div id="viewDiv"></div>
        <button id="drawRectButton">Draw Rectangle</button>
        <script src="https://js.arcgis.com/4.25/"></script>
        <script src="app.js"></script>
    </body>
    </html>

3、初始化地图视图:在app.js文件中初始化地图视图。

    require([
        "esri/Map",
        "esri/views/MapView",
        "esri/Graphic",
        "esri/layers/GraphicsLayer",
        "esri/geometry/Polygon",
        "esri/symbols/SimpleFillSymbol",
        "esri/Color",
        "dojo/domReady!"
    ], function(Map, MapView, Graphic, GraphicsLayer, Polygon, SimpleFillSymbol, Color) {
        var map = new Map({
            basemap: "streets"
        });
        var view = new MapView({
            container: "viewDiv",
            map: map,
            center: [-118.80500, 34.02700], // Longitude, Latitude
            zoom: 13
        });
        // Create a graphics layer to hold the rectangle
        var graphicsLayer = new GraphicsLayer();
        map.add(graphicsLayer);
    });

实现绘制矩形功能

1、添加事件监听器:为按钮添加点击事件监听器,当点击按钮时调用绘制矩形的函数。

arcgisjs画矩形

    document.getElementById("drawRectButton").addEventListener("click", drawRectangle);

2、定义绘制矩形的函数:在app.js中定义drawRectangle函数,该函数将在地图上绘制一个预设大小的矩形。

    function drawRectangle() {
        require([
            "esri/geometry/SpatialReference",
            "esri/geometry/Point",
            "esri/geometry/util/GeometryEngine"
        ], function(SpatialReference, Point, GeometryEngine) {
            // Define the coordinates of the rectangle corners (in Web Mercator projection)
            var point1 = new Point({longitude: -118.805, latitude: 34.027}, SpatialReference.create(4326));
            var point2 = new Point({longitude: -118.800, latitude: 34.030}, SpatialReference.create(4326));
            var point3 = new Point({longitude: -118.800, latitude: 34.025}, SpatialReference.create(4326));
            var point4 = new Point({longitude: -118.805, latitude: 34.025}, SpatialReference.create(4326));
            // Project the points to the map's spatial reference (Web Mercator)
            point1 = GeometryEngine.project(point1, map.spatialReference);
            point2 = GeometryEngine.project(point2, map.spatialReference);
            point3 = GeometryEngine.project(point3, map.spatialReference);
            point4 = GeometryEngine.project(point4, map.spatialReference);
            // Create a polygon from the points
            var polygon = new Polygon({
                rings: [[point1, point2, point3, point4]],
                srid: map.spatialReference.wkid,
                hasZ: false,
                hasM: false
            });
            // Create a symbol for the polygon
            var simpleFillSymbol = new SimpleFillSymbol({
                color: new Color([226, 119, 40]), // Orange color
                style: "solid",
                outline: { // autocasts as esri/symbols/SimpleLineSymbol
                    color: [255, 255, 255], // White color
                    width: "2px"
                }
            });
            // Create a graphic and add it to the graphics layer
            var rectangleGraphic = new Graphic({
                geometry: polygon,
                symbol: simpleFillSymbol,
                popupTemplate: { // autocasts as esri/PopupTemplate
                    title: "Rectangle",
                    content: "This is a drawn rectangle."
                }
            });
            graphicsLayer.add(rectangleGraphic);
        });
    }

完整代码示例

以下是完整的HTML和JavaScript代码示例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Draw Rectangle</title>
    <link rel="stylesheet" href="https://js.arcgis.com/4.25/esri/themes/light/main.css">
    <style>
        html, body, #viewDiv {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
        }
    </style>
</head>
<body>
    <div id="viewDiv"></div>
    <button id="drawRectButton">Draw Rectangle</button>
    <script src="https://js.arcgis.com/4.25/"></script>
    <script>
        require([
            "esri/Map",
            "esri/views/MapView",
            "esri/Graphic",
            "esri/layers/GraphicsLayer",
            "esri/geometry/Polygon",
            "esri/symbols/SimpleFillSymbol",
            "esri/Color",
            "esri/geometry/SpatialReference",
            "esri/geometry/Point",
            "esri/geometry/util/GeometryEngine",
            "dojo/domReady!"
        ], function(Map, MapView, Graphic, GraphicsLayer, Polygon, SimpleFillSymbol, Color, SpatialReference, Point, GeometryEngine) {
            var map = new Map({
                basemap: "streets"
            });
            var view = new MapView({
                container: "viewDiv",
                map: map,
                center: [-118.80500, 34.02700], // Longitude, Latitude
                zoom: 13
            });
            // Create a graphics layer to hold the rectangle
            var graphicsLayer = new GraphicsLayer();
            map.add(graphicsLayer);
            document.getElementById("drawRectButton").addEventListener("click", drawRectangle);
            function drawRectangle() {
                // Define the coordinates of the rectangle corners (in Web Mercator projection)
                var point1 = new Point({longitude: -118.805, latitude: 34.027}, SpatialReference.create(4326));
                var point2 = new Point({longitude: -118.800, latitude: 34.030}, SpatialReference.create(4326));
                var point3 = new Point({longitude: -118.800, latitude: 34.025}, SpatialReference.create(4326));
                var point4 = new Point({longitude: -118.805, latitude: 34.025}, SpatialReference.create(4326));
                // Project the points to the map's spatial reference (Web Mercator)
                point1 = GeometryEngine.project(point1, map.spatialReference);
                point2 = GeometryEngine.project(point2, map.spatialReference);
                point3 = GeometryEngine.project(point3, map.spatialReference);
                point4 = GeometryEngine.project(point4, map.spatialReference);
                // Create a polygon from the points
                var polygon = new Polygon({
                    rings: [[point1, point2, point3, point4]],
                    srid: map.spatialReference.wkid,
                    hasZ: false,
                    hasM: false
                });
                // Create a symbol for the polygon
                var simpleFillSymbol = new SimpleFillSymbol({
                    color: new Color([226, 119, 40]), // Orange color
                    style: "solid",
                    outline: { // autocasts as esri/symbols/SimpleLineSymbol
                        color: [255, 255, 255], // White color
                        width: "2px"
                    }
                });
                // Create a graphic and add it to the graphics layer
                var rectangleGraphic = new Graphic({
                    geometry: polygon,
                    symbol: simpleFillSymbol,
                    popupTemplate: { // autocasts as esri/PopupTemplate
                        title: "Rectangle",
                        content: "This is a drawn rectangle."
                    }
                });
                graphicsLayer.add(rectangleGraphic);
            }
        });
    </script>
</body>
</html>

相关问题与解答栏目

问题1:如何在ArcGIS JavaScript API中动态修改矩形的大小和位置?:答:要动态修改矩形的大小和位置,可以创建一个包含矩形坐标的变量,并在需要时更新这些变量的值,重新生成多边形并更新图形图层中的图形对象,可以在按钮点击事件中获取新的坐标值,并使用这些新值来更新矩形的位置和大小,具体实现可以参考以下代码片段:

function updateRectangle(newPoints) {
    require([
        "esri/geometry/SpatialReference",
        "esri/geometry/Point",
        "esri/geometry/util/GeometryEngine"
    ], function(SpatialReference, Point, GeometryEngine) {
        // Project the new points to the map's spatial reference (Web Mercator)
        var projectedPoints = newPoints.map(function(point) {
            return GeometryEngine.project(new Point(point), map.spatialReference);
        });
        // Create a new polygon from the updated points
        var newPolygon = new Polygon({
            rings: [projectedPoints],
            srid: map.spatialReference.wkid,
            hasZ: false,
            hasM: false
        });
        // Update the existing graphic with the new polygon geometry
        if (rectangleGraphic) {
            rectangleGraphic.geometry = newPolygon;
            rectangleGraphic.symbol = simpleFillSymbol; // Reapply the symbol if needed
        } else {
            rectangleGraphic = new Graphic({
                geometry: newPolygon,
                symbol: simpleFillSymbol,
                popupTemplate: { // autocasts as esri/PopupTemplate
                    title: "Updated Rectangle",
                    content: "This is an updated drawn rectangle."
                }
            });
            graphicsLayer.add(rectangleGraphic); // Add the new graphic to the layer if it doesn't exist yet
        }
    });
}

到此,以上就是小编对于“arcgisjs画矩形”的问题就介绍到这了,希望介绍的几点解答对大家有用,有任何问题和不懂的,欢迎各位朋友在评论区讨论,给我留言。

原创文章,作者:K-seo,如若转载,请注明出处:https://www.kdun.cn/ask/688840.html

Like (0)
Donate 微信扫一扫 微信扫一扫
K-seoK-seo
Previous 2024-11-29 11:53
Next 2024-11-29 11:55

相关推荐

  • 如何利用ArcGIS JS实现缩放到特定图层?

    一、引言ArcGIS JavaScript API 是 Esri 提供的一个强大的开发工具,用于在网页中创建动态的地图应用程序,它提供了丰富的功能接口,使得开发者能够轻松实现地图的各种交互和展示效果,“缩放到图层”是一个常见且实用的功能,允许用户根据特定图层的内容自动调整地图视图范围,二、基本概念与原理1. 缩……

    2024-11-30
    09
  • 如何在ArcGIS JS中实现文字标注功能?

    ArcGIS JS 文字标注指南1. 简介ArcGIS JavaScript API 是 Esri 提供的一个强大的工具,用于在 Web 应用程序中嵌入交互式地图,文字标注是地理信息系统(GIS)中的一个基本功能,它允许用户在地图上添加描述性文本,以提高地图的可读性和信息传递效果,本文将详细介绍如何使用 Arc……

    2024-11-28
    09
  • 如何使用ArcGIS JS绘制图形?

    使用 ArcGIS JavaScript API 画图形ArcGIS JavaScript API 是 Esri 提供的一个强大的工具集,用于在网页上构建交互式地图和应用程序,通过这个 API,开发者可以轻松地绘制各种几何图形,如点、线、面等,并对其进行操作,本文将详细介绍如何使用 ArcGIS JavaScr……

    2024-11-29
    06
  • 如何在ArcGIS JS中添加图层?

    使用ArcGIS JavaScript API添加图层ArcGIS JavaScript API 是 Esri 提供的一个功能强大的工具,用于在网页上创建动态地图应用程序,本文将详细介绍如何使用 ArcGIS JavaScript API 在 Web 应用中添加不同类型的图层,1. 引入ArcGIS JavaS……

    2024-11-29
    04
  • 如何在ArcGIS JS中添加图例?

    在ArcGIS JavaScript API中添加图例在使用ArcGIS JavaScript API开发地图应用时,添加图例(Legend)是一个常见的需求,图例可以帮助用户理解地图上的符号和颜色所代表的意义,本文将详细介绍如何在ArcGIS JavaScript API中添加图例,包括步骤、代码示例以及常见……

    2024-11-29
    08
  • 如何利用ArcGIS JS进行空间数据的统计分析?

    ArcGIS JS 统计功能概述ArcGIS JavaScript API 是 Esri 提供的一个强大的工具集,它允许开发者在 Web 应用中构建和操作地图,统计功能是 ArcGIS JS 提供的一项关键能力,可以帮助用户分析和理解地理数据,1. 统计数据的收集与展示在使用 ArcGIS JS 进行统计时,首……

    2024-11-29
    03

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

免备案 高防CDN 无视CC/DDOS攻击 限时秒杀,10元即可体验  (专业解决各类攻击)>>点击进入