Mapbox - Changing Map Style Causes Directions to Not Work Properly

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:

Example of issue: 

Git hub original post:

My Code:

Thanks for the help!

1 Answer

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest