如何检测地图移动的时间?

时间:2016-10-23 15:27:03

标签: javascript esri arcgis-js-api

我希望在中心位置和缩放方面检测地图的移动时间。

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

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);
});

1 个答案:

答案 0 :(得分:1)

嗯,您可以使用 esri watchUtils

主要取决于您使用的 ArcgGIS JS API 版本。直到 3.18 ,它与 extent-change 一起正常运作。根据您的代码,您似乎正在使用 4.0 or above

使用此代替 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>

希望这会对您有所帮助:)