ArcGIS JSAPI 学习教程 - ArcGIS Maps SDK for JavaScript - 框选显示高亮几何对象

news2024/7/6 19:13:32

ArcGIS JSAPI 学习教程 - ArcGIS Maps SDK for JavaScript - 框选显示高亮对象

    • 核心代码
    • 完整代码:
    • 在线示例

在研究 ArcGIS JSAPI RenderNode 高亮(highlights)FBO 的时候,实现了一下框选高亮几何对象,这里分享一下。

本文包括核心代码、完整代码以及在线示例。


核心代码

实际上,就是通过标绘工具,创建矩形标绘,在判断矩形相交,然后高亮相交的几何对象。


// 监听标绘完成事件
sketchViewModel.on("create", async (event) => {

    if (event.state === "complete") {

        const queryGeometry = event.graphic.geometry;

        if (this.campusLayerView) {

            // 获取矩形内几何对象
            const results = await this.campusLayerView.queryFeatures({
                geometry: queryGeometry,
            });

            // 设置高亮
            results.features.forEach((feature) => {
                this.highlights.push(this.campusLayerView.highlight([feature.attributes.OID]));
            })
        }
    }
});


完整代码:


<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
    <title>框选显示高亮对象 | Sample | ArcGIS Maps SDK for JavaScript 4.29</title>
    <script type="module" src="/arcgis_api/calcite-components/2.8.1/calcite.esm.js"></script>
    <link rel="stylesheet" type="text/css" href="/arcgis_api/calcite-components/2.8.1/calcite.css" />
    <link rel="stylesheet" href="/arcgis_api/4.29/esri/themes/light/main.css" />
    <script src="/arcgis_api/4.29/init.js"></script>
    <script>
        var _hmt = _hmt || [];
        (function () {
            var hm = document.createElement("script");
            hm.src = "https://hm.baidu.com/hm.js?f80a36f14f8a73bb0f82e0fdbcee3058";
            var s = document.getElementsByTagName("script")[0];
            s.parentNode.insertBefore(hm, s);
        })();
    </script>
    <script>
        require([
                "esri/WebScene",
                "esri/views/SceneView",
                "esri/rest/support/Query",
                "esri/widgets/Legend",
                "esri/core/reactiveUtils",
                "esri/views/3d/webgl/RenderNode",
                "esri/layers/GraphicsLayer",
                "esri/widgets/Sketch/SketchViewModel",

            ],
            (
                WebScene,
                SceneView,
                Query,
                Legend,
                reactiveUtils,
                RenderNode,
                GraphicsLayer,
                SketchViewModel,

            ) => {
                // 使用官方资源
                const webscene = new WebScene({
                    portalItem: {
                        // autocasts as new PortalItem()
                        id: "fbbc829fa7d342e7ae8d18c54a5eab37"
                    }
                });

                // Create a view and set the highlight options
                const view = new SceneView({
                    container: "viewDiv",
                    map: webscene,
                    popup: {
                        dockOptions: {
                            buttonEnabled: false
                        }
                    },
                    qualityProfile: "high",
                    environment: {
                        lighting: {
                            directShadowsEnabled: true
                        }
                    },
                    // 设置默认高亮参数
                    highlightOptions: {
                        haloColor: [255, 38, 150],
                        color: [255, 255, 255],
                        fillOpacity: 0.3
                    }
                });

                // This variable will store the highlight handle that is used to remove the highlight
                this.highlights = [];

                // 创建标绘图层
                const polygonGraphicsLayer = new GraphicsLayer({
                    elevationInfo: {
                        // 注意,这里设置相对于地形,否则会遮挡
                        mode: 'relative-to-ground'
                    }
                });
                view.map.add(polygonGraphicsLayer);

                // add the select by rectangle button the view
                view.ui.add("select-by-rectangle", "top-left");
                const selectButton = document.getElementById("select-by-rectangle");

                // 创建标绘工具
                const sketchViewModel = new SketchViewModel({
                    view: view,
                    layer: polygonGraphicsLayer
                });

                // 监听矩形标绘事件
                selectButton.addEventListener("click", () => {

                    view.closePopup();

                    // 标绘矩形
                    sketchViewModel.create("rectangle");

                    // 移除上一次操作
                    polygonGraphicsLayer.removeAll();

                    // 移除上一次高亮对象
                    if(this.highlights){

                        this.highlights.forEach((highlight) => {
                            highlight.remove();
                        });
                        this.highlights = [];
                    }
                });

                // 监听标绘完成事件
                sketchViewModel.on("create", async (event) => {

                    if (event.state === "complete") {

                        const queryGeometry = event.graphic.geometry;

                        if (this.campusLayerView) {

                            // 获取矩形内几何对象
                            const results = await this.campusLayerView.queryFeatures({
                                geometry: queryGeometry,
                            });

                            // 设置高亮
                            results.features.forEach((feature) => {
                                this.highlights.push(this.campusLayerView.highlight([feature.attributes.OID]));
                            })
                        }
                    }
                });

                view.when(() => {

                    // Get layer from webScene
                    const campusSceneLayer = webscene.allLayers.filter((elem) => {
                        return elem.title === "Buildings";
                    }).items[0];
                    // Define the attributes which are used in the query
                    campusSceneLayer.outFields = ["name"];

                    // Get DOM element where list items will be placed
                    const container = document.getElementById("buildingsList");
                    const buildingCount = document.getElementById("buildingCount");

                    // Highlight is set on the layerView, so we need to detect when the layerView is ready
                    view.whenLayerView(campusSceneLayer).then((campusLayerView) => {

                        this.campusLayerView = campusLayerView;

                        // Wait for the view to finish updating
                        reactiveUtils.when(() => !view.updating, () => {
                            // Query the features available for drawing and get the attributes
                            const query = new Query();
                            campusLayerView.queryFeatures(query).then((result) => {
                                // Empty the list DOM element
                                container.innerHTML = "";
                                buildingCount.innerHTML = `Currently in view: ${result.features.length} buildings`;
                                // For each returned feature create a list item and append it to the container
                                result.features.forEach((feature) => {
                                    const attributes = feature.attributes;
                                    // Create list element
                                    const li = document.createElement("calcite-pick-list-item");
                                    li.setAttribute("label", attributes.name);
                                    li.addEventListener("click", (event) => {
                                        const target = event.target;
                                        const objectId = feature.attributes.OID;
                                        // Create an extent query on the layer view that will return the 3D extent of the feature
                                        const queryExtent = new Query({
                                            objectIds: [objectId]
                                        });
                                        campusLayerView
                                            .queryExtent(queryExtent)
                                            .then((result) => {
                                                // Zoom to the extent of the feature
                                                // Use the expand method to prevent zooming in too close to the feature
                                                if (result.extent) {
                                                    view
                                                        .goTo(result.extent.expand(4), {
                                                            speedFactor: 0.5
                                                        })
                                                        .catch((error) => {
                                                            if (error.name != "AbortError") {
                                                                console.error(error);
                                                            }
                                                        });
                                                }
                                            });
                                        // Remove the previous highlights
                                        if (highlight) {
                                            highlight.remove();
                                        }
                                        // Highlight the feature passing the objectId to the method
                                        highlight = campusLayerView.highlight([objectId]);
                                    });
                                    container.appendChild(li);
                                });
                            });
                        });
                    });
                });

            });
    </script>
    <style>
        html,
        body,
        #viewDiv {
            height: 100%;
            width: 100%;
            margin: 0;
            padding: 0;
        }

        .panel-side {
            width: 250px;
            position: absolute;
            top: 14px;
            right: 14px;
            bottom: 28px;
            color: #323232;
            background-color: rgb(255, 255, 255);
            box-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
            overflow: auto;
            z-index: 60;
            font-size: 12px;
            text-align: center;
        }

        .panel-side h2 {
            padding: 0 20px;
            margin: 20px 0;
            font-size: 14px;
            font-weight: 600;
        }

        #buildingCount,
        h2 {
            text-align: center;
        }
    </style>
</head>

<body>
<div class="panel-side esri-widget">
    <h2>Campus buildings</h2>
    <p id="buildingCount">Currently in view: 0 buildings</p>
    <calcite-panel id="buildingsList">
    </calcite-panel>
</div>
<div id="viewDiv"></div>
<div
        id="select-by-rectangle"
        class="esri-widget esri-widget--button esri-widget esri-interactive"
        title="Select features by rectangle"
>
    <span class="esri-icon-checkbox-unchecked"></span>
</div>
</body>

</html>


在这里插入图片描述


在线示例

ArcGIS Maps SDK for JavaScript 在线示例:框选显示高亮对象

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1799089.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

springboot配置集成RedisTemplate和Redisson,使用分布式锁案例

文章要点 自定义配置属性类集成配置RedisTemplate集成配置分布式锁Redisson使用分布式锁简单实现超卖方案 1. 项目结构 2. 集成RedisTemplate和Redisson 添加依赖 依赖的版本与继承的spring-boot-starter-parent工程相对应&#xff0c;可写可不写 <!--spring data redis…

【SpringBoot + Vue 尚庭公寓实战】租期管理接口实现(四)

【SpringBoot Vue 尚庭公寓实战】租期管理接口实现&#xff08;四&#xff09; 文章目录 【SpringBoot Vue 尚庭公寓实战】租期管理接口实现&#xff08;四&#xff09;1、查询全部租期列表2、保存或更新租期信息3、根据ID删除租期 租期管理共有三个接口&#xff0c;分别是 保…

备份和恢复realme智能手机:综合指南

realme自2018年成立至今&#xff0c;一直秉持着“敢于超越”的品牌精神&#xff0c;专注于为全球年轻用户提供性能卓越、设计新颖的高品质手机。对于如何备份和恢复realme手机&#xff0c;本文将介绍多种不同的方法。 第1部分&#xff1a;使用Coolmuster Android Backup Mana…

Android Lottie 体积优化实践:从 6.4 MB 降到 530 KB

一、说明 产品提出需求&#xff1a;用户有 8 个等级&#xff0c;每个等级对应一个奖牌动画。 按照常用的实现方式&#xff1a; 设计提供 8 个 lottie 动画&#xff08;8 个 json 文件&#xff09;。研发将 json 文件打包进入 APK 中。根据不同等级播放指定的动画。 每一个 …

【动态规划-BM69 把数字翻译成字符串】

题目 BM69 把数字翻译成字符串 描述 有一种将字母编码成数字的方式&#xff1a;‘a’->1, ‘b->2’, … , ‘z->26’。 现在给一串数字&#xff0c;返回有多少种可能的译码结果 分析 特判一个‘0’的情况 后面可以用动态规划&#xff1a; dp[n]为考虑前n个字符时&…

一分钟了解香港的场外期权报价

香港的场外期权报价 在香港这个国际金融中心&#xff0c;场外期权交易是金融市场不可或缺的一部分。场外期权&#xff0c;作为一种非标准化的金融衍生品&#xff0c;为投资者提供了在特定时间以约定价格买入或卖出某种资产的机会。对于希望参与这一市场的投资者来说&#xff0…

LeetCode62不同路径

题目描述 一个机器人位于一个 m x n 网格的左上角 &#xff08;起始点在下图中标记为 “Start” &#xff09;。机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角&#xff08;在下图中标记为 “Finish” &#xff09;。问总共有多少条不同的路径&#xff1f; …

LeetCode 两数之和 + 三数之和

两数之和 简单题 思路&#xff1a;一个Map&#xff0c;key是数值&#xff0c;value是该数值对应的下标&#xff0c;遍历的时候判断一下当前数组下标对应的值在map里有没有可组合成target的&#xff08;具体体现为在map里找target-nums【i】)&#xff0c;如果有&#xff0c;直接…

STL中stack和queue模拟实现+容器适配器

目录 容器适配器 STL标准库中stack和queue的底层结构 deque的简单介绍 deque的缺陷 为什么选择deque作为stack和queue的底层默认容器 stack的模拟实现 queue的模拟实现 容器适配器 适配器是一种设计模式&#xff08;设计模式是一套被反复使用的&#xff0c;多数人知晓…

2024北京消防展6.26召开-看消防安全企业如何升级赋能

2024北京消防展6.26召开-看消防安全企业如何升级赋能 随着社会的快速发展&#xff0c;消防安全已经成为企业安全生产的重要一环。作为消防领域的品质盛会&#xff0c;2024中国&#xff08;北京&#xff09;消防技术与设备展览会将于6月26-28 日在北京.首钢会展中心召开&#xf…

Django 传递额外参数给视图函数

本书1-7章样章及配套资源下载链接: https://pan.baidu.com/s/1OGmhHxEMf2ZdozkUnDkAkA?pwdnanc 源码、PPT课件、教学视频等&#xff0c;可以从前言给出的下载信息下载&#xff0c;大家可以评估一下。 在Django框架中&#xff0c;URLconf模块还支持一种传递额外参数给视图函…

生活使用英语口语柯桥外语学校成人英语学习

● “自来水”英语怎么说&#xff1f; ● “自来水”的英语表达是&#xff1a;Running water或者Tap water. 例句&#xff1a; There are hot and cold running water in all the bedrooms. 所有的卧室里都有冷热自来水。 ● “热水”英文怎么水&#xff1f; ● 我们不管…

upload-labs-第一关和第二关

目录 第一关 思路&#xff1a; 1、上传一个php文件 2、查看源码 3、查看文件上传地址 4、BP抓包&#xff0c;修改文件后缀名 5、使用蚁剑连接 第二关 1、这一关也可以跟第一关一样的方法进行绕过 2、上传一个一句话木马文件 第一关 原理&#xff1a; 思路&#xff1a…

Pyramid Vision Transformer, PVT(ICCV 2021)原理与代码解读

paper&#xff1a;Pyramid Vision Transformer: A Versatile Backbone for Dense Prediction without Convolutions official implementation&#xff1a;GitHub - whai362/PVT: Official implementation of PVT series 存在的问题 现有的 Vision Transformer (ViT) 主要设计…

一键生成迷宫-Word插件-大珩助手新功能

Word大珩助手是一款功能丰富的Office Word插件&#xff0c;旨在提高用户在处理文档时的效率。它具有多种实用的功能&#xff0c;能够帮助用户轻松修改、优化和管理Word文件&#xff0c;从而打造出专业而精美的文档。 【新功能】迷宫生成器 1、可自定义迷宫大小&#xff1b; …

【第二节】C/C++数据结构之线性表

目录 一、线性表基本说明 1.1 基本概念 1.2 抽象数据类型 1.3 存储结构 1.4 插入与删除的区别 1.5 顺序存储和链式存储的优缺点 二、链表 2.1 基本概念 2.2 抽象数据类型 2.3 单链表的定义 2.4 单链表的基本操作 2.5 单链表模板形式的类定义与实现 三、单向循环链…

SpringBoot高手之路03-事务传播行为

那么就是 a事务调用了b事务 日志技术 当解散部门的时候,那么就直接进行 操作日志 就是什么时间点吧部门解散 成功失败都需要记录日志 首先一个日志表 那么日志技术,在电商平台,不论是否支付订单,那么都需要保存订单信息 这个时候我们就使用传播事务 传播事务首先是出现在两…

AI重塑搜索和浏览器,360打造AIPC轻量化方案

6月6日&#xff0c;360AI新品发布会暨开发者沟通会在京举办&#xff0c;360集团发布全新360AI搜索、360AI浏览器&#xff0c;360集团创始人周鸿祎在现场使用360AI搜索为2024年高考语文作文押题。同时&#xff0c;“360AI甄选”平台及会员体系“360AI大会员”正式上线&#xff0…

前端开发常用的工具和软件,提高编程效率

目录 1. 文本编辑器与IDE (集成开发环境)2. 版本控制工具3. 构建工具与包管理器4. 前端框架与库5. 设计与原型工具6. 测试与调试工具7. 代码协作与项目管理8. 自动化部署与持续集成/持续部署(CI/CD)相关链接&#xff1a; 前端开发过程中使用的工具和软件种类繁多&#xff0c;可…

「动态规划」如何求地下城游戏中,最低初始健康点数是多少?

174. 地下城游戏https://leetcode.cn/problems/dungeon-game/description/ 恶魔们抓住了公主并将她关在了地下城dungeon的右下角。地下城是由m x n个房间组成的二维网格。我们英勇的骑士最初被安置在左上角的房间里&#xff0c;他必须穿过地下城并通过对抗恶魔来拯救公主。骑士…