当前位置: 首页 > news >正文

ArcGIS for js 4.x FeatureLayer 加载、点选、高亮

安装arcgis for js 4.x 依赖:

npm install @arcgis/core

一、FeatureLayer 加载

代码如下:

<template><view id="mapView"></view></template><script setup>import "@arcgis/core/assets/esri/themes/light/main.css";
import Map from '@arcgis/core/Map.js';
import MapView from '@arcgis/core/views/MapView.js';
import TileLayer from '@arcgis/core/layers/TileLayer.js'
import WebTileLayer from '@arcgis/core/layers/WebTileLayer.js'
import FeatureLayer from "@arcgis/core/layers/FeatureLayer.js"
import Graphic from "@arcgis/core/Graphic.js";
import SimpleFillSymbol from "@arcgis/core/symbols/SimpleFillSymbol.js";
import { onMounted } from "vue";onMounted(()=>{initTDTMap();});// 天地图加载
const initTDTMap = () =>{let webLayer = new WebTileLayer({urlTemplate: "http://{subDomain}.tianditu.gov.cn/vec_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=vec&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILECOL={col}&TILEROW={row}&TILEMATRIX={level}&tk=352d4b1313777a8643542046a28d17e5",subDomains: ['t0', 't1', 't2', 't3', 't4', 't5', 't6', 't7']});const map = new Map({basemap:{baseLayers:[webLayer]}});const mapView = new MapView({// 默认中心点位center: [125.338, 43.882],map: map,// 初始层级zoom: 10,container: "mapView",constraints: {minZoom: 9,// 最小层级maxZoom: 17// 最大层级},});// 清除底部powered by ESRI
mapView.ui.remove("attribution");// 加载FeatureLayerconst featureLayer = new FeatureLayer({url:"http://116.141.0.114:48080/geoscene/rest/services/%E8%80%95%E4%BF%9D/%E5%8F%8C%E9%98%B3%E5%8C%BA%E5%9B%BE%E6%96%91/FeatureServer/0",outFields: ["*"], //加载所有要素字段opacity:0.5,//透明度//popupTemplate: TuCeng03TC, //加载模板,//definitionExpression: "",// 筛查// 渲染renderer: {type: "simple",symbol: {type: "simple-fill",//simple-line(线)、simple-fill(面)style: "solid",// solid 全部填充、cross十字交错、diamond菱形、square矩形、triangle三角形color: [255,20,60, 0.4],outline: {color: [255, 0, 0, 1],width: 2,},},}});// 添加featureLayer(两种方法都可以)//mapView.map.add(featureLayer);map.add(featureLayer);</script><style lang="scss" scoped>#mapView{width: 100%;height:100vh;}</style>

二、点选FeatureLayer

代码如下:

    // featureLayer图斑点击事件mapView.on(['click'],function(event){mapView.hitTest(event).then(function(response){if (response.results.length) {let clickLayer = response.results.filter((result) => {return result.graphic.layer === featureLayer;});// 获取绘制let graphic = clickLayer[0].graphic;// 点击后定位mapView.goTo(graphic);// 如果点选到图层上的要素,则输出要素的IDvar objId = response.results.map(function(result) {return result.graphic.attributes[result.graphic.layer.objectIdField];});console.log("点选到的要素ID: ", objId);// 如果点选到图层上的要素,则输出全部属性var attributes = response.results.map(function(result) {return result.graphic.attributes;});console.log("点选到的attributes: ", attributes);}else{console.log("点选无果");}})});

三、高亮

1、默认高亮

	// featureLayer图斑点击事件let highlight;// 默认高亮对象mapView.on(['click'],function(event){mapView.hitTest(event).then(function(response){// 清空上一次高亮-默认 /* if(highlight)highlight.remove(); */// 清空上一次高亮-自定义mapView.graphics.removeAll();if (response.results.length) {let clickLayer = response.results.filter((result) => {return result.graphic.layer === featureLayer;});// 获取绘制let graphic = clickLayer[0].graphic;// 点击后定位mapView.goTo(graphic);/* // 如果点选到图层上的要素,则输出要素的IDvar objId = response.results.map(function(result) {return result.graphic.attributes[result.graphic.layer.objectIdField];});console.log("点选到的要素ID: ", objId);// 如果点选到图层上的要素,则输出全部属性var attributes = response.results.map(function(result) {return result.graphic.attributes;});console.log("点选到的attributes: ", attributes);*/// 默认选中高亮mapView.whenLayerView(graphic.layer).then((layerView)=>{highlight = layerView.highlight(graphic)});// 自定义选中高亮/* var symbol = new SimpleFillSymbol({color: [255, 255, 0, 0.5], // 黄色半透明填充outline: {color: [255, 0, 0], // 红色边框width: 2}});graphic.symbol = symbol;mapView.graphics.add(graphic); */}else{console.log("点选无果");}})});

2、自定义高亮

// featureLayer图斑点击事件//let highlight;// 默认高亮对象mapView.on(['click'],function(event){mapView.hitTest(event).then(function(response){// 清空上一次高亮-默认 /* if(highlight)highlight.remove(); */// 清空上一次高亮-自定义mapView.graphics.removeAll();if (response.results.length) {let clickLayer = response.results.filter((result) => {return result.graphic.layer === featureLayer;});// 获取绘制let graphic = clickLayer[0].graphic;// 点击后定位mapView.goTo(graphic);/* // 如果点选到图层上的要素,则输出要素的IDvar objId = response.results.map(function(result) {return result.graphic.attributes[result.graphic.layer.objectIdField];});console.log("点选到的要素ID: ", objId);// 如果点选到图层上的要素,则输出全部属性var attributes = response.results.map(function(result) {return result.graphic.attributes;});console.log("点选到的attributes: ", attributes);*/// 默认选中高亮/* mapView.whenLayerView(graphic.layer).then((layerView)=>{highlight = layerView.highlight(graphic)}); */// 自定义选中高亮var symbol = new SimpleFillSymbol({color: [255, 255, 0, 0.5], // 黄色半透明填充outline: {color: [255, 0, 0], // 红色边框width: 2}});graphic.symbol = symbol;mapView.graphics.add(graphic);}else{console.log("点选无果");}})});

http://www.lryc.cn/news/368441.html

相关文章:

  • 倩女幽魂手游攻略:云手机自动搬砖辅助教程!
  • Typesense-开源的轻量级搜索引擎
  • 探索 LLM 预训练的挑战,GPU 集群架构实战
  • 高考分数查询结果自动推送至微信(卷II)
  • python类动态属性,以属性方式访问字典
  • 招聘在家抄书员?小心是骗局!!!
  • Pytorch学习11_神经网络-卷积层
  • Qt实现程序单实例运行(只能运行1个进程)及QSharedMemory用法
  • HTTP协议分析实验:通过一次下载任务抓包分析
  • http网络服务器
  • 使用C++结合OpenCV进行图像处理与分类
  • 探索 Noisee AI 的奇妙世界与变现之旅
  • 【SCSS】use的详细使用规则
  • 数据结构(C):二叉树前中后序和层序详解及代码实现及深度刨析
  • Win11可以安装AutoCAD2007
  • C#操作MySQL从入门到精通(14)——汇总数据
  • 【设计模式深度剖析】【2】【行为型】【命令模式】| 以打开文件按钮、宏命令、图形移动与撤销为例加深理解
  • 【随手记】maplotlib.use函数设置图像的呈现方式
  • LLVM Cpu0 新后端 系列课程总结
  • 【云原生】Kubernetes----RBAC用户资源权限
  • ORA-01652 表空间不够解决方案
  • 亚马逊 AWS 视频转码功能、AWS Elemental MediaConvert 中创建和管理转码作业
  • RocketMQ可视化界面安装
  • 【ffmpeg】本地格式转换 mp4转wav||裁剪mp4
  • 基于Django+MySQL的智慧校园系统
  • Linux基础指令(一)
  • 三极管十大品牌
  • 需求记录(共享元素)
  • .Net 使用 MongoDB
  • 【TensorFlow深度学习】值函数估计:蒙特卡洛方法与TD学习