Mapbox - Changing map style causes Directions to not work properly

1.2k Views Asked by At

Having an issue with getting Directions to work in MapBox web application issue happens when I change the map style. This question has been asked in github, but has not received been resolved yet. (From 2018) Hoping that by now there is a solution to why this is happening.

If you change a maps style with directions already rendered you end up with: Error: The layer 'directions-route-line-alt' does not exist in the map's style and cannot be queried for features.

(From github question - link below)
This example was generated from the directions example: https://www.mapbox.com/mapbox-gl-js/example/mapbox-gl-directions/

Example of issue: 
https://plnkr.co/edit/M8mj1WTH1KjDa1dhiUKw?p=preview&preview

Git hub original post: https://github.com/mapbox/mapbox-gl-js/issues/7019

My Code: https://github.com/juanmguevara/MapBoxMapping

Thanks for the help!

1

There are 1 best solutions below

0
Steve Bennett On

When you add the MapboxDirections control to the map, it adds an extra layer or two.

When you "change a map's style", you're essentially removing all the existing sources and layers, and adding new ones.

So it is removing the layers you added at the start, which then causes the error you see.

After you change the map's style, you should remove and re-add the MapboxDirections control.