Mapbox-gl-js: How to load data after map style is loaded

Created on 15 Mar 2016  路  4Comments  路  Source: mapbox/mapbox-gl-js

mapbox-gl-js version: latest version

getting error like "Style not done loading" often. Can you please tell me how to avoid this?

Most helpful comment

Even if the that event is triggered, your map could be not full ready.
It's an issue in MapBox, I needed to do this to solve my problem

myMap.on('style.load', () => {
  const waiting = () => {
    if (!myMap.isStyleLoaded()) {
      setTimeout(waiting, 200);
    } else {
      loadMyLayers();
    }
  };
  waiting();
});

All 4 comments

Source/layer manipulations can only be done after the style is loaded, so you have to wrap you code in map.on('style.load', function () { ... }) handler. See examples, e.g. this one: https://www.mapbox.com/mapbox-gl-js/example/geojson-layer-in-stack/

Even if the that event is triggered, your map could be not full ready.
It's an issue in MapBox, I needed to do this to solve my problem

myMap.on('style.load', () => {
  const waiting = () => {
    if (!myMap.isStyleLoaded()) {
      setTimeout(waiting, 200);
    } else {
      loadMyLayers();
    }
  };
  waiting();
});

I have been trying to figure out how to do this without a timeout. My issue was the text was not rendering by the time the on load event was called. So instead of using map.on('load'), i would use map.on('idle').

map.on('idle', () => {
   // map.getCanvas().toDataURL()
}
Was this page helpful?
0 / 5 - 0 ratings