javascript - 如何检测 map 何时移动?

标签 javascript esri arcgis-js-api

我希望检测 map 何时移动,包括中心位置和缩放。

这就是我目前正在尝试的方式:

var map = new Map({
    basemap: 'streets'
});

var view = new MapView({
    container: 'mapDiv',
    map: map
});

map.on('extent-change', function () {
    console.log('Map is moved', arguments);
});

最佳答案

好吧,您可以为此使用 esri watchUtils

这主要取决于您使用的 ArcgGIS JS API 版本。直到 3.18,它都可以使用 extent-change 正常工作。根据您的代码,您似乎使用的是 4.0 或更高版本

使用它代替 extent-change-

 watchUtils.whenTrue(view, "stationary", function() {        
      // Get the new extent of view/map whenever map is updated. 
      if (view.extent) {
        console.log('Map is moved');
      }
 });

下面是这个的工作代码 -

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
  <title>Get started with MapView - Create a 2D map - 4.1</title>
  <style>
    html,
    body,
    #viewDiv {
      padding: 0;
      margin: 0;
      height: 100%;
      width: 100%;
    }
    
        
    #outputMessages {
      position: absolute;
      z-index: 70;
      top: 0px;
      right: 0;
      bottom: 0;
      box-sizing: border-box;
      padding: 20px;
      height: 100%;
      width: 350px;
      background: rgba(0, 0, 0, 0.7);
      color: white;
      font-size: 14px;
      line-height: 30px;
      overflow: auto
    }
    
    #outputMessages span {
      color: #F7BE81;
      font-weight: bold;
    }
  </style>

  <link rel="stylesheet" href="https://js.arcgis.com/4.1/esri/css/main.css">
  <script src="https://js.arcgis.com/4.1/"></script>

  <script>
    require([
      "esri/Map",
      "dojo/dom",
      "esri/views/MapView",
      "esri/core/watchUtils",
      "dojo/domReady!"
    ], function(Map,dom, MapView, watchUtils) {

      var map = new Map({
        basemap: "streets"
      });

      var view = new MapView({
        container: "viewDiv",
        map: map,
        zoom: 4,
        center: [15, 65]
      });
      
            var outputMessages = dom.byId("outputMessages");
      function displayMessage(info) {
        outputMessages.innerHTML += info;
        outputMessages.scrollTop = outputMessages.scrollHeight;
      }
      
    //*******************************************************************
      // Watching properties for changes  
      // 1. Watch view.stationary property 
      //*******************************************************************
      // Watch view's stationary property for becoming true. 
      watchUtils.whenTrue(view, "stationary", function() {
        // Get the new center of the view only when view is stationary. 
        if (view.center) {
          var info = "<br> <span> the view center changed. </span> x: " +
            view.center.x.toFixed(2) + " y: " + view.center.y.toFixed(2);
          displayMessage(info);
        }

        // Get the new extent of the view only when view is stationary. 
        if (view.extent) {
          var info = "<br> <span> the view extent changed: </span>" +
            "<br> xmin:" + view.extent.xmin.toFixed(2) + " xmax: " +
            view.extent.xmax.toFixed(2) +
            "<br> ymin:" + view.extent.ymin.toFixed(2) + " ymax: " +
            view.extent.ymax.toFixed(2);
          displayMessage(info);
        }
      });


    });
  </script>
</head>

<body>
  <div id="viewDiv">
     <div id="outputMessages"></div>
  </div>
</body>
</html>

希望这对您有所帮助:)

关于javascript - 如何检测 map 何时移动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40204590/

相关文章:

javascript - 如何保证一个html页面上所有的元素都加载完毕,然后我们在CSS中对元素进行一些修改呢?

javascript - jQuery Each Loop - 显示单个项目

javascript - 我可以使用条件来渲染网格小部件吗?

javascript - arcgis featureLayer 的替代品?

java - Mapreduce java 程序搜索 QuadTree 索引并运行 GeometryEngine.contains 以使用 wkt 文件确认多边形中的点

ios - 在 Esri map 中单击位置按钮时移动到当前位置

javascript - 当所有数据加载时如何运行函数?

javascript - arcgis中如何更改信息窗口的位置

javascript - 数学轮函数

javascript - 在 Jquery 中将 CSS 属性应用于 html