首页 > 解决方案 > 如何区分 map.fitBounds 触发的传单缩放和用户鼠标滚轮?

问题描述

我有一张地图,每 30 秒刷新一次,每次刷新时,我都适合新数据的界限。如果用户操纵了地图,例如更改缩放级别,我想更改适合边界的方式。我的问题是触发的缩放fitBounds与用户操作无法区分。

如何mousewheel在地图上捕获/扩展?

标签: leaflet

解决方案


基本上有多种相同的方法,但您可以使用以下简单的方法来区分用户缩放触发。

onwheel当鼠标滚轮在元素上向上或向下滚动时发生该事件。

var map = L.map('map', {
    // Set latitude and longitude of the map center (required)
    center: [12.99766, -84.90838],
    // Set the initial zoom level, values 0-18, where 0 is most zoomed-out (required)
    zoom: 5,
 // scrollWheelZoom: false
});

// Create a Tile Layer and add it to the map
var tiles = new L.tileLayer('https://{s}.tile.openstreetmap.fr/hot/{z}/{x}/{y}.png').addTo(map);


document.getElementById("map").addEventListener("wheel", myFunction);

function myFunction() {
  alert("Mouse wheel Scrolled by user.")
}
#map {
  width: 500px;
  height: 400px;
}
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.0.3/dist/leaflet.css" />
<script src="https://unpkg.com/leaflet@1.0.3/dist/leaflet.js"></script>


<div id="map"></div>

希望这会帮助你。


推荐阅读