Mapbox: How to avoid JavaScript errors for tilesets that aren't available at the current zoom level?
Asked Answered
S

3

6

I'm using Mapbox GL JS and loading tileset layers from my Mapbox account. Some of these tileset layers are only available for zoom levels 10 to 15.

The default zoom level of my map is 5, and when I load the map I get a JavaScript console error, saying that the tileset is 404ing:

enter image description here

Is there any way I can avoid this? I don't want to recreate the tileset all the way to zoom level 5, as it will unnecessarily increase its size.

I don't think the console error is causing any problems in Chrome, but I don't know whether it will in other browsers.

Sancho answered 13/3, 2017 at 20:9 Comment(3)
I cannot reproduce this error in the latest release. What version are you using?Beer
I was using 0.32 - just upgraded to 0.33. I still see errors about missing tiles, but is it possible that these are tiles that are missing for some other reason rather than zoom level?Sancho
In case it helps anyone else, I found out that the tiles were 404ing because they'd failed during the tippecanoe tile creation process, because they were over 500k when converted.Sancho
A
8

The easiest way is to replace the default error handler, filtering out the "Not Found" message:

map.on('error', e => {
    // Hide those annoying non-error errors
    if (e && e.error !== 'Error: Not Found')
        console.error(e);
});
Attain answered 14/3, 2017 at 4:11 Comment(2)
Accepting since this solves the symptom, thank you! To solve the underlying problem, please see my comment on the original question.Sancho
Cool - I find this symptom pops up pretty often even with tilesets hosted on Mapbox, as soon as you pan outside the range of the tileset.Attain
B
2

I have improved our 404 handling for future releases.

In this case, you will still see the browser-provided GET https://... 404 (Not Found) message but not the Javascript Error: Not Found exception message.

Beer answered 20/3, 2017 at 18:53 Comment(0)
S
2

If you are using your own tile server you can set it up to give a No Content 204 HTTP status.

Here is what it would like in a custom made node.js tile server:

app.use(function(req, res, next) {
  if(res.status(404)) {
    res.sendStatus(204)
  }
});
Sliwa answered 7/6, 2018 at 16:23 Comment(1)
According to the corresponding GitHub ticket this is the preferred solution by the Mapbox devs.Maitund

© 2022 - 2024 — McMap. All rights reserved.