mapbox.js icon indicating copy to clipboard operation
mapbox.js copied to clipboard

When setting any map options, tiles param is required

Open mapmeld opened this issue 8 years ago • 3 comments

For adding a new MapBox studio layer to a Mapbox.js slippy map, documentation suggests this code:

var map = L.mapbox.map('map')
    .setView([38.97416, -95.23252], 15);
L.mapbox.styleLayer('mapbox://styles/mapbox/emerald-v8').addTo(map);

I have a map with option {zoomControl: false} so I made this change:

var map = L.mapbox.map('map', { zoomControl: false })
    .setView([38.97416, -95.23252], 15);
L.mapbox.styleLayer('mapbox://styles/mapbox/emerald-v8').addTo(map);

But it throws an error on that line Uncaught TypeError: Cannot read property '0' of undefined because Mapbox.js is expecting a tiles option to be present as an array, if I included any other options.

Also the tiles option cannot understand the mapbox:// format, so I needed to include a {z} {x} {y} tile URL.

What I'd like to see changed:

  • mapbox:// format gets accepted in any tile layer option
  • tiles is a more-optional map option

mapmeld avatar Jun 28 '17 00:06 mapmeld

I came here to report this issue as well.

Thanks for explaining what is required to make options work. I also found #640 helpful (apparently you can do L.mapbox.map('map', 'mapbox.streets', {...options}) and that will work too).

It's pretty disappointing that there was an issue about this 5 years ago and the docs still aren't correct.

sdhull avatar Apr 19 '19 06:04 sdhull

Just realized passing 'mapbox.streets' kinda screws up the style layer ... so you can also just pass false as the 2nd param there & add the style layer as usual

sdhull avatar Apr 19 '19 06:04 sdhull

I solved this by calling L.map(...) instead of L.mapbox.map(...).

L.mapbox.map extends from L.map. The former expects 3 parameters L.mapbox.map(element, id|url|tilejson, options) and the latter only two L.map(element, options)

After that I "manually" set the layer using map.addLayer(L.mapbox.styleLayer(style));

So in the end it lloks like this:

const map = L.map(element[0], mapOptions);
map.addLayer(L.mapbox.styleLayer(style));

iursevla avatar May 14 '20 14:05 iursevla