Extend / override Mapbox-gl 'baselayer' loadTile










2















Similar to how Leaflet's guide shows how to override L.TileLayer's getTileUrl method in place of your own (and L.GridLayer's createTile method),



I want to know how to extend/override Mapbox-gl's baselayer tiles (as opposed to only using a tile server URL to add in custom tiles).



I'm looking to avoid just adding a layer and instead want to overwrite the baselayer itself.



I saw this issue discussion of customizing RasterTileSource.loadTile and wondered if this was the right direction.



Leaflet's example:



L.TileLayer.Kitten = L.TileLayer.extend(
getTileUrl: function(coords)
var i = Math.ceil( Math.random() * 4 );
return "https://placekitten.com/256/256?image=" + i;

);

L.tileLayer.kitten = function()
return new L.TileLayer.Kitten();


L.tileLayer.kitten().addTo(map);









share|improve this question
























  • I assume you're asking about mapbox gl js instead of mapbox.js because of the tags for the question

    – AndrewHarvey
    Nov 15 '18 at 13:58











  • @AndrewHarvey yes! I will make things clearer.

    – Alex
    Nov 15 '18 at 15:18






  • 1





    Have you tried asking in Geographic Information Systems? A different (although overlapping) set of people hang out there.

    – peeebeee
    Nov 21 '18 at 18:16











  • @peeebeee Thank you for the advice! Here's that post: gis.stackexchange.com/questions/303555/…

    – Alex
    Nov 22 '18 at 1:38















2















Similar to how Leaflet's guide shows how to override L.TileLayer's getTileUrl method in place of your own (and L.GridLayer's createTile method),



I want to know how to extend/override Mapbox-gl's baselayer tiles (as opposed to only using a tile server URL to add in custom tiles).



I'm looking to avoid just adding a layer and instead want to overwrite the baselayer itself.



I saw this issue discussion of customizing RasterTileSource.loadTile and wondered if this was the right direction.



Leaflet's example:



L.TileLayer.Kitten = L.TileLayer.extend(
getTileUrl: function(coords)
var i = Math.ceil( Math.random() * 4 );
return "https://placekitten.com/256/256?image=" + i;

);

L.tileLayer.kitten = function()
return new L.TileLayer.Kitten();


L.tileLayer.kitten().addTo(map);









share|improve this question
























  • I assume you're asking about mapbox gl js instead of mapbox.js because of the tags for the question

    – AndrewHarvey
    Nov 15 '18 at 13:58











  • @AndrewHarvey yes! I will make things clearer.

    – Alex
    Nov 15 '18 at 15:18






  • 1





    Have you tried asking in Geographic Information Systems? A different (although overlapping) set of people hang out there.

    – peeebeee
    Nov 21 '18 at 18:16











  • @peeebeee Thank you for the advice! Here's that post: gis.stackexchange.com/questions/303555/…

    – Alex
    Nov 22 '18 at 1:38













2












2








2


1






Similar to how Leaflet's guide shows how to override L.TileLayer's getTileUrl method in place of your own (and L.GridLayer's createTile method),



I want to know how to extend/override Mapbox-gl's baselayer tiles (as opposed to only using a tile server URL to add in custom tiles).



I'm looking to avoid just adding a layer and instead want to overwrite the baselayer itself.



I saw this issue discussion of customizing RasterTileSource.loadTile and wondered if this was the right direction.



Leaflet's example:



L.TileLayer.Kitten = L.TileLayer.extend(
getTileUrl: function(coords)
var i = Math.ceil( Math.random() * 4 );
return "https://placekitten.com/256/256?image=" + i;

);

L.tileLayer.kitten = function()
return new L.TileLayer.Kitten();


L.tileLayer.kitten().addTo(map);









share|improve this question
















Similar to how Leaflet's guide shows how to override L.TileLayer's getTileUrl method in place of your own (and L.GridLayer's createTile method),



I want to know how to extend/override Mapbox-gl's baselayer tiles (as opposed to only using a tile server URL to add in custom tiles).



I'm looking to avoid just adding a layer and instead want to overwrite the baselayer itself.



I saw this issue discussion of customizing RasterTileSource.loadTile and wondered if this was the right direction.



Leaflet's example:



L.TileLayer.Kitten = L.TileLayer.extend(
getTileUrl: function(coords)
var i = Math.ceil( Math.random() * 4 );
return "https://placekitten.com/256/256?image=" + i;

);

L.tileLayer.kitten = function()
return new L.TileLayer.Kitten();


L.tileLayer.kitten().addTo(map);






leaflet mapbox raster mapbox-gl-js wms






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 15:19







Alex

















asked Nov 15 '18 at 4:32









AlexAlex

34549




34549












  • I assume you're asking about mapbox gl js instead of mapbox.js because of the tags for the question

    – AndrewHarvey
    Nov 15 '18 at 13:58











  • @AndrewHarvey yes! I will make things clearer.

    – Alex
    Nov 15 '18 at 15:18






  • 1





    Have you tried asking in Geographic Information Systems? A different (although overlapping) set of people hang out there.

    – peeebeee
    Nov 21 '18 at 18:16











  • @peeebeee Thank you for the advice! Here's that post: gis.stackexchange.com/questions/303555/…

    – Alex
    Nov 22 '18 at 1:38

















  • I assume you're asking about mapbox gl js instead of mapbox.js because of the tags for the question

    – AndrewHarvey
    Nov 15 '18 at 13:58











  • @AndrewHarvey yes! I will make things clearer.

    – Alex
    Nov 15 '18 at 15:18






  • 1





    Have you tried asking in Geographic Information Systems? A different (although overlapping) set of people hang out there.

    – peeebeee
    Nov 21 '18 at 18:16











  • @peeebeee Thank you for the advice! Here's that post: gis.stackexchange.com/questions/303555/…

    – Alex
    Nov 22 '18 at 1:38
















I assume you're asking about mapbox gl js instead of mapbox.js because of the tags for the question

– AndrewHarvey
Nov 15 '18 at 13:58





I assume you're asking about mapbox gl js instead of mapbox.js because of the tags for the question

– AndrewHarvey
Nov 15 '18 at 13:58













@AndrewHarvey yes! I will make things clearer.

– Alex
Nov 15 '18 at 15:18





@AndrewHarvey yes! I will make things clearer.

– Alex
Nov 15 '18 at 15:18




1




1





Have you tried asking in Geographic Information Systems? A different (although overlapping) set of people hang out there.

– peeebeee
Nov 21 '18 at 18:16





Have you tried asking in Geographic Information Systems? A different (although overlapping) set of people hang out there.

– peeebeee
Nov 21 '18 at 18:16













@peeebeee Thank you for the advice! Here's that post: gis.stackexchange.com/questions/303555/…

– Alex
Nov 22 '18 at 1:38





@peeebeee Thank you for the advice! Here's that post: gis.stackexchange.com/questions/303555/…

– Alex
Nov 22 '18 at 1:38












1 Answer
1






active

oldest

votes


















1














You can use map.transformRequest to intercept and modify a tile request. For instance:



function addAuthHeaders(url, resourceType) 
var username = 'me';
var password = 'password';
if (resourceType === 'Tile' && url.match('mysite'))
return
url: url,
headers: 'Authorization': 'Basic ' + btoa(username + ':' + password)
;



var map = mapboxgl.Map(
…,
transformRequest: addAuthHeaders
);


Note however, that this feature is poorly documented.






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312478%2fextend-override-mapbox-gl-baselayer-loadtile%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You can use map.transformRequest to intercept and modify a tile request. For instance:



    function addAuthHeaders(url, resourceType) 
    var username = 'me';
    var password = 'password';
    if (resourceType === 'Tile' && url.match('mysite'))
    return
    url: url,
    headers: 'Authorization': 'Basic ' + btoa(username + ':' + password)
    ;



    var map = mapboxgl.Map(
    …,
    transformRequest: addAuthHeaders
    );


    Note however, that this feature is poorly documented.






    share|improve this answer



























      1














      You can use map.transformRequest to intercept and modify a tile request. For instance:



      function addAuthHeaders(url, resourceType) 
      var username = 'me';
      var password = 'password';
      if (resourceType === 'Tile' && url.match('mysite'))
      return
      url: url,
      headers: 'Authorization': 'Basic ' + btoa(username + ':' + password)
      ;



      var map = mapboxgl.Map(
      …,
      transformRequest: addAuthHeaders
      );


      Note however, that this feature is poorly documented.






      share|improve this answer

























        1












        1








        1







        You can use map.transformRequest to intercept and modify a tile request. For instance:



        function addAuthHeaders(url, resourceType) 
        var username = 'me';
        var password = 'password';
        if (resourceType === 'Tile' && url.match('mysite'))
        return
        url: url,
        headers: 'Authorization': 'Basic ' + btoa(username + ':' + password)
        ;



        var map = mapboxgl.Map(
        …,
        transformRequest: addAuthHeaders
        );


        Note however, that this feature is poorly documented.






        share|improve this answer













        You can use map.transformRequest to intercept and modify a tile request. For instance:



        function addAuthHeaders(url, resourceType) 
        var username = 'me';
        var password = 'password';
        if (resourceType === 'Tile' && url.match('mysite'))
        return
        url: url,
        headers: 'Authorization': 'Basic ' + btoa(username + ':' + password)
        ;



        var map = mapboxgl.Map(
        …,
        transformRequest: addAuthHeaders
        );


        Note however, that this feature is poorly documented.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 13 '18 at 3:34









        Steve BennettSteve Bennett

        50.9k22103136




        50.9k22103136





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312478%2fextend-override-mapbox-gl-baselayer-loadtile%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            政党

            天津地下鉄3号線