ArcGIS Maps SDK for JavaScript:鼠标进入面要素时改变鼠标指针样式,离开时恢复

文章目录

在 ArcGIS Maps SDK for JavaScript 中,可以通过监听 pointer-move 事件来检测鼠标是否进入了面要素,并且通过设置 CSS 样式来改变鼠标指针的样式。

1、关键代码

js 复制代码
// Listen for pointer-move event on the view
view.on("pointer-move", function(event) {
   // Check if the pointer is over a feature
   view.hitTest(event).then(function(response) {
     if (response.results.length && response.results[0].graphic.layer === featureLayer) {
       // Change cursor style to pointer if over a feature
       view.container.style.cursor = "pointer";
     } else {
       // Reset cursor style to default if not over a feature
       view.container.style.cursor = "default";
     }
   });
 });

2、完整示例

以下是一个简单的示例代码,演示了如何实现这一功能:

  • 创建了一个基本的地图 (map) 和地图视图 (view)。
  • 加载了一个要素图层 (featureLayer),可以根据自己的需求替换为自己的图层。
  • 监听了 pointer-move 事件,当鼠标移动时进行 hitTest 来判断是否悬停在要素上。
  • 如果鼠标悬停在要素上,则将视图容器 (view.container) 的鼠标样式 (cursor) 设置为 "pointer",表示指针样式;如果不是,则将鼠标样式设置为 "default",表示默认样式。
javascript 复制代码
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
  <title>ArcGIS JavaScript Tutorials: Change Cursor on Hover</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>
  <script src="https://js.arcgis.com/4.25/"></script>
  <script>
    require([
      "esri/Map",
      "esri/views/MapView",
      "esri/layers/FeatureLayer",
      "dojo/domReady!"
    ], function(Map, MapView, FeatureLayer) {

      // Create a map
      var map = new Map({
        basemap: "streets-navigation-vector"
      });

      // Create a MapView
      var view = new MapView({
        container: "viewDiv",
        map: map,
        center: [-118, 34],
        zoom: 8
      });

      // Create a feature layer
      var featureLayer = new FeatureLayer({
        url: "https://services.arcgis.com/V6ZHFr6zdgNZuVG0/arcgis/rest/services/USA_Counties/FeatureServer/0"
      });

      // Add the feature layer to the map
      map.add(featureLayer);

      // Listen for pointer-move event on the view
      view.on("pointer-move", function(event) {
        // Check if the pointer is over a feature
        view.hitTest(event).then(function(response) {
          if (response.results.length && response.results[0].graphic.layer === featureLayer) {
            // Change cursor style to pointer if over a feature
            view.container.style.cursor = "pointer";
          } else {
            // Reset cursor style to default if not over a feature
            view.container.style.cursor = "default";
          }
        });
      });

    });
  </script>
</head>
<body>
  <div id="viewDiv"></div>
</body>
</html>
相关推荐
一 乐13 分钟前
基于vue船运物流管理系统设计与实现(源码+数据库+文档)
前端·javascript·数据库·vue.js·spring boot·后端·船运系统
m0_5287238123 分钟前
在React中使用redux
前端·javascript·react.js
傻小胖35 分钟前
vue3中customRef的用法以及使用场景
前端·javascript·vue.js
Edward-tan3 小时前
【玩转全栈】--创建一个自己的vue项目
前端·javascript·vue.js
青年夏日科技工作者3 小时前
虚幻浏览器插件 UE与JS通信
前端·javascript·html
鸿业远图科技4 小时前
北京门头沟区房屋轮廓shp的arcgis数据建筑物轮廓无偏移坐标测评
arcgis
雷神乐乐4 小时前
创建前端项目的方法
前端·javascript·vue.js
prince_zxill4 小时前
JavaScript面向对象编程:Prototype与Class的对比详解
前端·javascript·ecmascript·原型模式
D.eL5 小时前
Vue 2 项目中 Mock.js 的完整集成与使用教程
前端·javascript·vue.js
prince_zxill6 小时前
Node.js 和 npm 安装教程
前端·javascript·vue.js·npm·node.js