Is there any idle event for mapbox gl js?
Asked Answered
N

4

10

I need some sort of google maps "idle" event for mapbox gl. When every event fired and the map stop zoomin/out drag etc. and every layer has loaded, and the map is idle. I have to use this code

  map.on("render", function(e) {
            if(map.loaded() && triggerOnce === true) {
//fires on zoomin runing
                triggerOnce = false;
                console.log("Render end")
                setTimeout(somefunc(),1000)
            }
          })
Naval answered 11/4, 2018 at 16:41 Comment(0)
B
7

Yes, as of mapbox-gl-js v0.52.0 there is now an idle event you can use. According to the docs:

Fired after the last frame rendered before the map enters an "idle" state:

  • No camera transitions are in progress
  • All currently requested tiles have loaded
  • All fade/transition animations have completed

To use it:

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

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

Demo: http://jsfiddle.net/godoshian/yrf0b9xt/

// 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);
#demo {
  display: flex;
  flex-direction: row;
  justify-content: space-evenly;
}
#demo #output-container {
  flex: 1;
}
#demo .map {
  height: 300px;
  flex: 2;
}
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.52.0/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.52.0/mapbox-gl.css' rel='stylesheet' />

<div id="demo">
  <div id="output-container">
  </div>

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

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

Relevant PR: https://github.com/mapbox/mapbox-gl-js/pull/7625

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

Bootleg answered 11/1, 2019 at 3:51 Comment(0)
C
3

just listen to moveend event, it will be fired after any moves on the map like dragging, zooming, rotating and pitching.

Casseycassi answered 3/6, 2018 at 16:52 Comment(0)
S
1

idle, not working, because it fire event every 1 second, continues trigger event.

 map.on('idle', (e) => { 

use moveend event instead.

     map.on('moveend', (e) => { 
Selftaught answered 21/5, 2019 at 20:30 Comment(1)
Untrue. Run the code snippet of the accepted answer which proves the idle event is not triggering continuously when the map is untouched.Vehement
T
0

You can have similar event by using setTimeout to monitor the onViewPortChange event

var changingViewPortTimeout; 

onViewportChange(viewport) {
    if (changingViewPortTimeout) {
      clearTimeout(changingViewPortTimeout);
    }
    changingViewPortTimeout = setTimeout(function () {
        onIdle(viewport);
      }, 200)
    });
}
Teaching answered 26/6, 2018 at 9:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.