mapbox gl js有空闲事件吗?

SER*_*ERG 7 mapbox mapbox-gl

我需要某种谷歌地图"空闲"事件为mapbox gl.当每个事件都被触发并且地图停止了zoomin/out拖动等并且每个图层都已加载,并且地图处于空闲状态.我必须使用此代码

  map.on("render", function(e) {
            if(map.loaded() && triggerOnce === true) {
//fires on zoomin runing
                triggerOnce = false;
                console.log("Render end")
                setTimeout(somefunc(),1000)
            }
          })
Run Code Online (Sandbox Code Playgroud)

小智 5

是的,从 mapbox-gl-js v0.52.0 开始,您可以使用空闲事件。根据文档:

在地图进入“空闲”状态之前渲染的最后一帧之后触发:

  • 没有正在进行的相机转换
  • 当前请求的所有图块都已加载
  • 所有淡入淡出/过渡动画已完成

要使用它:

map.once('idle', (e) => {
    // do things the first time the map idles
});

map.on('idle', (e) => {
    // do things every time the map idles
});
Run Code Online (Sandbox Code Playgroud)

演示:http : //jsfiddle.net/godoshian/yrf0b9xt/

map.once('idle', (e) => {
    // do things the first time the map idles
});

map.on('idle', (e) => {
    // do things every time the map idles
});
Run Code Online (Sandbox Code Playgroud)
// Data from http://geojson.xyz/
const geojsonSource = 'https://d2ad6b4ur7yvpq.cloudfront.net/naturalearth-3.3.0/ne_50m_populated_places.geojson';
const outputContainer = document.getElementById('output-container');

mapboxgl.accessToken = 'pk.eyJ1IjoiY2NoYW5nc2EiLCJhIjoiY2lqeXU3dGo1MjY1ZXZibHp5cHF2a3Q1ZyJ9.8q-mw77HsgkdqrUHdi-XUg';

function createMap(container, layer = null) {
	const map = new mapboxgl.Map({
    container,
    style: 'mapbox://styles/mapbox/light-v9',
  });
  map.on('idle', () => {
    outputContainer.innerHTML += `${container} idle<br>`;
  });
  if (layer) {
  	map.on('load', () => {
    	map.addLayer(layer);
    });
  }
  return map;
}

const map = createMap('map1');


setTimeout(() => {
	fetch(geojsonSource)
    .then(response => {
      if (response.ok) return response.json();
      throw Error(response);
    })
    .then(json => {
      let layer = {
        id: 'populated-places',
        source: {
          type: 'geojson',
          data: json,
        },
        type: 'circle',
      }
      map.addLayer(layer);
      createMap('map2', layer);
    })
    .catch(error => {
    	console.log(error);
    })
}, 5000);
Run Code Online (Sandbox Code Playgroud)
#demo {
  display: flex;
  flex-direction: row;
  justify-content: space-evenly;
}
#demo #output-container {
  flex: 1;
}
#demo .map {
  height: 300px;
  flex: 2;
}
Run Code Online (Sandbox Code Playgroud)

相关公关:https : //github.com/mapbox/mapbox-gl-js/pull/7625

文档:https ://www.mapbox.com/mapbox-gl-js/api/#map.event: idle