Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does not load traffic layer by default #9

Open
AndreyNazarchuk opened this issue Feb 1, 2020 · 1 comment
Open

Does not load traffic layer by default #9

AndreyNazarchuk opened this issue Feb 1, 2020 · 1 comment

Comments

@AndreyNazarchuk
Copy link

AndreyNazarchuk commented Feb 1, 2020

This can not show traffic by default, I have to press the traffic button twice to cycle it. there does not seem to be a working method to show traffic on load.

My code looks like this:

mapboxgl.accessToken = 'redacted_privatekey_'; var map = new mapboxgl.Map({ container: 'map', style: 'mapbox://styles/mapbox/streets-v11', zoom: 8, center: [-122, 47.8], }); map.on('load', function() { map.addControl(new mapboxgl.NavigationControl()); map.addControl(new MapboxTraffic({showTraffic: true})); });

@cemelo
Copy link

cemelo commented May 11, 2023

For anyone having the same issue, you need to call the render function after the map is loaded:

  mapboxgl.accessToken = 'redacted_privatekey_';
  const map = new mapboxgl.Map({
    container: 'map',
    style: 'mapbox://styles/mapbox/streets-v11',
    zoom: 8,
    center: [-122, 47.8]
  });
  
  const traffic = new MapboxTraffic({ showTraffic: true });
  
  map.on('load', () => {
    map.addControl(new mapboxgl.NavigationControl());
    map.addControl(traffic);
    
    traffic.render();
  });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants