How to update an XYZ layer in Open Layers 5









up vote
1
down vote

favorite












Is removing and re-adding an XYZ layer the only way to update it in ol5?



I know TileWMS has an updateParams() method, which is nice, but some of the layers I'm working with are XYZ with a time query parameter.



Here is the pseudo code for how I've worked around it, but it doesn't seem like the right way to go about it.



function createLayer() 
return new TileLayer(
source: new XYZ(
url: 'https://url?x=x&y=y&z=z&time=' + dateTimeString,
)
);

map.addLayer(createLayer());
// user interaction to change the time
map.removeLayer(createLayer());
map.addLayer(createLayer());


Thanks!










share|improve this question

























    up vote
    1
    down vote

    favorite












    Is removing and re-adding an XYZ layer the only way to update it in ol5?



    I know TileWMS has an updateParams() method, which is nice, but some of the layers I'm working with are XYZ with a time query parameter.



    Here is the pseudo code for how I've worked around it, but it doesn't seem like the right way to go about it.



    function createLayer() 
    return new TileLayer(
    source: new XYZ(
    url: 'https://url?x=x&y=y&z=z&time=' + dateTimeString,
    )
    );

    map.addLayer(createLayer());
    // user interaction to change the time
    map.removeLayer(createLayer());
    map.addLayer(createLayer());


    Thanks!










    share|improve this question























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      Is removing and re-adding an XYZ layer the only way to update it in ol5?



      I know TileWMS has an updateParams() method, which is nice, but some of the layers I'm working with are XYZ with a time query parameter.



      Here is the pseudo code for how I've worked around it, but it doesn't seem like the right way to go about it.



      function createLayer() 
      return new TileLayer(
      source: new XYZ(
      url: 'https://url?x=x&y=y&z=z&time=' + dateTimeString,
      )
      );

      map.addLayer(createLayer());
      // user interaction to change the time
      map.removeLayer(createLayer());
      map.addLayer(createLayer());


      Thanks!










      share|improve this question













      Is removing and re-adding an XYZ layer the only way to update it in ol5?



      I know TileWMS has an updateParams() method, which is nice, but some of the layers I'm working with are XYZ with a time query parameter.



      Here is the pseudo code for how I've worked around it, but it doesn't seem like the right way to go about it.



      function createLayer() 
      return new TileLayer(
      source: new XYZ(
      url: 'https://url?x=x&y=y&z=z&time=' + dateTimeString,
      )
      );

      map.addLayer(createLayer());
      // user interaction to change the time
      map.removeLayer(createLayer());
      map.addLayer(createLayer());


      Thanks!







      openlayers openlayers-5






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 10 at 15:42









      Michael

      614513




      614513






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          This method can be used either as a dummy parameter to override caching or to set a configurable time parameter on sources such as weather maps:



          var layer = new TileLayer(
          source: new XYZ()
          );
          function setTileUrl(dateTime)
          layer.getSource().setUrl('https://url?x=x&y=y&z=z&time=' + dateTime);

          setTileUrl(initialDateTime);
          map.addLayer(layer);
          // user interaction to change the time
          setTileUrl(newDateTime);





          share|improve this answer



























            up vote
            2
            down vote













            Another way would be to refresh the source in connection with using a tileUrlFunction:



            TileLayer.getSource().refresh();


            See also here.






            share|improve this answer


















            • 2




              That would only clear the Openlayers cache, it won't re-evaluate the url. TileLayer.getSource().setUrl('https://url?x=x&y=y&z=z&time=' + dateTimeString); will do both (the time parameter will still be needed to override the browser cache).
              – Mike
              Nov 10 at 18:33











            • You are right. In connection with a tileUrlFunction it works for me.
              – RalphL
              Nov 11 at 16:50










            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',
            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%2f53240548%2fhow-to-update-an-xyz-layer-in-open-layers-5%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            3
            down vote



            accepted










            This method can be used either as a dummy parameter to override caching or to set a configurable time parameter on sources such as weather maps:



            var layer = new TileLayer(
            source: new XYZ()
            );
            function setTileUrl(dateTime)
            layer.getSource().setUrl('https://url?x=x&y=y&z=z&time=' + dateTime);

            setTileUrl(initialDateTime);
            map.addLayer(layer);
            // user interaction to change the time
            setTileUrl(newDateTime);





            share|improve this answer
























              up vote
              3
              down vote



              accepted










              This method can be used either as a dummy parameter to override caching or to set a configurable time parameter on sources such as weather maps:



              var layer = new TileLayer(
              source: new XYZ()
              );
              function setTileUrl(dateTime)
              layer.getSource().setUrl('https://url?x=x&y=y&z=z&time=' + dateTime);

              setTileUrl(initialDateTime);
              map.addLayer(layer);
              // user interaction to change the time
              setTileUrl(newDateTime);





              share|improve this answer






















                up vote
                3
                down vote



                accepted







                up vote
                3
                down vote



                accepted






                This method can be used either as a dummy parameter to override caching or to set a configurable time parameter on sources such as weather maps:



                var layer = new TileLayer(
                source: new XYZ()
                );
                function setTileUrl(dateTime)
                layer.getSource().setUrl('https://url?x=x&y=y&z=z&time=' + dateTime);

                setTileUrl(initialDateTime);
                map.addLayer(layer);
                // user interaction to change the time
                setTileUrl(newDateTime);





                share|improve this answer












                This method can be used either as a dummy parameter to override caching or to set a configurable time parameter on sources such as weather maps:



                var layer = new TileLayer(
                source: new XYZ()
                );
                function setTileUrl(dateTime)
                layer.getSource().setUrl('https://url?x=x&y=y&z=z&time=' + dateTime);

                setTileUrl(initialDateTime);
                map.addLayer(layer);
                // user interaction to change the time
                setTileUrl(newDateTime);






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 10 at 20:52









                Mike

                51916




                51916






















                    up vote
                    2
                    down vote













                    Another way would be to refresh the source in connection with using a tileUrlFunction:



                    TileLayer.getSource().refresh();


                    See also here.






                    share|improve this answer


















                    • 2




                      That would only clear the Openlayers cache, it won't re-evaluate the url. TileLayer.getSource().setUrl('https://url?x=x&y=y&z=z&time=' + dateTimeString); will do both (the time parameter will still be needed to override the browser cache).
                      – Mike
                      Nov 10 at 18:33











                    • You are right. In connection with a tileUrlFunction it works for me.
                      – RalphL
                      Nov 11 at 16:50














                    up vote
                    2
                    down vote













                    Another way would be to refresh the source in connection with using a tileUrlFunction:



                    TileLayer.getSource().refresh();


                    See also here.






                    share|improve this answer


















                    • 2




                      That would only clear the Openlayers cache, it won't re-evaluate the url. TileLayer.getSource().setUrl('https://url?x=x&y=y&z=z&time=' + dateTimeString); will do both (the time parameter will still be needed to override the browser cache).
                      – Mike
                      Nov 10 at 18:33











                    • You are right. In connection with a tileUrlFunction it works for me.
                      – RalphL
                      Nov 11 at 16:50












                    up vote
                    2
                    down vote










                    up vote
                    2
                    down vote









                    Another way would be to refresh the source in connection with using a tileUrlFunction:



                    TileLayer.getSource().refresh();


                    See also here.






                    share|improve this answer














                    Another way would be to refresh the source in connection with using a tileUrlFunction:



                    TileLayer.getSource().refresh();


                    See also here.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 11 at 16:52

























                    answered Nov 10 at 16:06









                    RalphL

                    213




                    213







                    • 2




                      That would only clear the Openlayers cache, it won't re-evaluate the url. TileLayer.getSource().setUrl('https://url?x=x&y=y&z=z&time=' + dateTimeString); will do both (the time parameter will still be needed to override the browser cache).
                      – Mike
                      Nov 10 at 18:33











                    • You are right. In connection with a tileUrlFunction it works for me.
                      – RalphL
                      Nov 11 at 16:50












                    • 2




                      That would only clear the Openlayers cache, it won't re-evaluate the url. TileLayer.getSource().setUrl('https://url?x=x&y=y&z=z&time=' + dateTimeString); will do both (the time parameter will still be needed to override the browser cache).
                      – Mike
                      Nov 10 at 18:33











                    • You are right. In connection with a tileUrlFunction it works for me.
                      – RalphL
                      Nov 11 at 16:50







                    2




                    2




                    That would only clear the Openlayers cache, it won't re-evaluate the url. TileLayer.getSource().setUrl('https://url?x=x&y=y&z=z&time=' + dateTimeString); will do both (the time parameter will still be needed to override the browser cache).
                    – Mike
                    Nov 10 at 18:33





                    That would only clear the Openlayers cache, it won't re-evaluate the url. TileLayer.getSource().setUrl('https://url?x=x&y=y&z=z&time=' + dateTimeString); will do both (the time parameter will still be needed to override the browser cache).
                    – Mike
                    Nov 10 at 18:33













                    You are right. In connection with a tileUrlFunction it works for me.
                    – RalphL
                    Nov 11 at 16:50




                    You are right. In connection with a tileUrlFunction it works for me.
                    – RalphL
                    Nov 11 at 16:50

















                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240548%2fhow-to-update-an-xyz-layer-in-open-layers-5%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

                    27

                    Top Tejano songwriter Luis Silva dead of heart attack at 64

                    Category:Rhetoric