Adding multiple Leaflet layers resulted in inaccurately positioned containers











up vote
2
down vote

favorite












I have the following code to extend Leaflet layer for adding custom layers.



L.CustomLayer = L.Layer.extend({
initialize: function (latlng, options) {
this._latlng = L.latLng(latlng);
L.Util.setOptions(this, options);
},
onAdd: function (map) {
this._div = document.createElement("div");
this._div.style.padding = "8px";
this._div.style.border = "2px solid grey";
this._div.style.borderRadius = "2px";
this.getPane().appendChild(this._div);
this._update();
map.on("zoomend viewreset", this._update, this);
},
onRemove: function (map) {
L.DomUtil.remove(this._div);
map.off("zoomend viewreset", this._update, this);
},
_update: function () {
var pos = this._map.latLngToLayerPoint(this._latlng);
L.DomUtil.setPosition(this._div, pos);
}
});

L.customLayer = function (latlng, options) {
return new L.CustomLayer(latlng, options);
};


However, when I tried to add two markers and two custom layers at the same coordinates respectively, the second layer seems to be positioned wrongly.





The layers are added with the following code:



var customLayer1 = L.customLayer().setPosition([3.139003, 101.686852]).addTo(map);
var customLayer2 = L.customLayer().setPosition([6.121070, 100.369797]).addTo(map);


Any advice on this? CodePen is here. Thanks for your help!










share|improve this question




























    up vote
    2
    down vote

    favorite












    I have the following code to extend Leaflet layer for adding custom layers.



    L.CustomLayer = L.Layer.extend({
    initialize: function (latlng, options) {
    this._latlng = L.latLng(latlng);
    L.Util.setOptions(this, options);
    },
    onAdd: function (map) {
    this._div = document.createElement("div");
    this._div.style.padding = "8px";
    this._div.style.border = "2px solid grey";
    this._div.style.borderRadius = "2px";
    this.getPane().appendChild(this._div);
    this._update();
    map.on("zoomend viewreset", this._update, this);
    },
    onRemove: function (map) {
    L.DomUtil.remove(this._div);
    map.off("zoomend viewreset", this._update, this);
    },
    _update: function () {
    var pos = this._map.latLngToLayerPoint(this._latlng);
    L.DomUtil.setPosition(this._div, pos);
    }
    });

    L.customLayer = function (latlng, options) {
    return new L.CustomLayer(latlng, options);
    };


    However, when I tried to add two markers and two custom layers at the same coordinates respectively, the second layer seems to be positioned wrongly.





    The layers are added with the following code:



    var customLayer1 = L.customLayer().setPosition([3.139003, 101.686852]).addTo(map);
    var customLayer2 = L.customLayer().setPosition([6.121070, 100.369797]).addTo(map);


    Any advice on this? CodePen is here. Thanks for your help!










    share|improve this question


























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I have the following code to extend Leaflet layer for adding custom layers.



      L.CustomLayer = L.Layer.extend({
      initialize: function (latlng, options) {
      this._latlng = L.latLng(latlng);
      L.Util.setOptions(this, options);
      },
      onAdd: function (map) {
      this._div = document.createElement("div");
      this._div.style.padding = "8px";
      this._div.style.border = "2px solid grey";
      this._div.style.borderRadius = "2px";
      this.getPane().appendChild(this._div);
      this._update();
      map.on("zoomend viewreset", this._update, this);
      },
      onRemove: function (map) {
      L.DomUtil.remove(this._div);
      map.off("zoomend viewreset", this._update, this);
      },
      _update: function () {
      var pos = this._map.latLngToLayerPoint(this._latlng);
      L.DomUtil.setPosition(this._div, pos);
      }
      });

      L.customLayer = function (latlng, options) {
      return new L.CustomLayer(latlng, options);
      };


      However, when I tried to add two markers and two custom layers at the same coordinates respectively, the second layer seems to be positioned wrongly.





      The layers are added with the following code:



      var customLayer1 = L.customLayer().setPosition([3.139003, 101.686852]).addTo(map);
      var customLayer2 = L.customLayer().setPosition([6.121070, 100.369797]).addTo(map);


      Any advice on this? CodePen is here. Thanks for your help!










      share|improve this question















      I have the following code to extend Leaflet layer for adding custom layers.



      L.CustomLayer = L.Layer.extend({
      initialize: function (latlng, options) {
      this._latlng = L.latLng(latlng);
      L.Util.setOptions(this, options);
      },
      onAdd: function (map) {
      this._div = document.createElement("div");
      this._div.style.padding = "8px";
      this._div.style.border = "2px solid grey";
      this._div.style.borderRadius = "2px";
      this.getPane().appendChild(this._div);
      this._update();
      map.on("zoomend viewreset", this._update, this);
      },
      onRemove: function (map) {
      L.DomUtil.remove(this._div);
      map.off("zoomend viewreset", this._update, this);
      },
      _update: function () {
      var pos = this._map.latLngToLayerPoint(this._latlng);
      L.DomUtil.setPosition(this._div, pos);
      }
      });

      L.customLayer = function (latlng, options) {
      return new L.CustomLayer(latlng, options);
      };


      However, when I tried to add two markers and two custom layers at the same coordinates respectively, the second layer seems to be positioned wrongly.





      The layers are added with the following code:



      var customLayer1 = L.customLayer().setPosition([3.139003, 101.686852]).addTo(map);
      var customLayer2 = L.customLayer().setPosition([6.121070, 100.369797]).addTo(map);


      Any advice on this? CodePen is here. Thanks for your help!







      javascript leaflet






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 at 6:17









      Foo

      1




      1










      asked Nov 12 at 6:15









      imckl0117

      112




      112
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          For those who might need this piece of information, I have found out the problem. You need to set the position of the container to absolute by doing:



          onAdd: function (map) {
          this._div = document.createElement("div");
          this._div.style.padding = "8px";
          this._div.style.border = "2px solid grey";
          this._div.style.borderRadius = "2px";

          // Set "position" to "absolute" manually for "top" and "left"
          // to work. Alternatively, apply CSS styles for leaflet-layer
          // class by adding class for all the necessary styles
          // this._div.style.position = "absolute";

          // See the link below for all the styles included
          // https://github.com/Leaflet/Leaflet/blob/master/dist/leaflet.css
          L.DomUtil.addClass(this._div, "leaflet-layer");

          this.getPane().appendChild(this._div);
          this._update();
          map.on("zoomend viewreset", this._update, this);
          },


          See the link to the leaflet.css for the necessary styles. :D






          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',
            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%2f53256760%2fadding-multiple-leaflet-layers-resulted-in-inaccurately-positioned-containers%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








            up vote
            0
            down vote



            accepted










            For those who might need this piece of information, I have found out the problem. You need to set the position of the container to absolute by doing:



            onAdd: function (map) {
            this._div = document.createElement("div");
            this._div.style.padding = "8px";
            this._div.style.border = "2px solid grey";
            this._div.style.borderRadius = "2px";

            // Set "position" to "absolute" manually for "top" and "left"
            // to work. Alternatively, apply CSS styles for leaflet-layer
            // class by adding class for all the necessary styles
            // this._div.style.position = "absolute";

            // See the link below for all the styles included
            // https://github.com/Leaflet/Leaflet/blob/master/dist/leaflet.css
            L.DomUtil.addClass(this._div, "leaflet-layer");

            this.getPane().appendChild(this._div);
            this._update();
            map.on("zoomend viewreset", this._update, this);
            },


            See the link to the leaflet.css for the necessary styles. :D






            share|improve this answer

























              up vote
              0
              down vote



              accepted










              For those who might need this piece of information, I have found out the problem. You need to set the position of the container to absolute by doing:



              onAdd: function (map) {
              this._div = document.createElement("div");
              this._div.style.padding = "8px";
              this._div.style.border = "2px solid grey";
              this._div.style.borderRadius = "2px";

              // Set "position" to "absolute" manually for "top" and "left"
              // to work. Alternatively, apply CSS styles for leaflet-layer
              // class by adding class for all the necessary styles
              // this._div.style.position = "absolute";

              // See the link below for all the styles included
              // https://github.com/Leaflet/Leaflet/blob/master/dist/leaflet.css
              L.DomUtil.addClass(this._div, "leaflet-layer");

              this.getPane().appendChild(this._div);
              this._update();
              map.on("zoomend viewreset", this._update, this);
              },


              See the link to the leaflet.css for the necessary styles. :D






              share|improve this answer























                up vote
                0
                down vote



                accepted







                up vote
                0
                down vote



                accepted






                For those who might need this piece of information, I have found out the problem. You need to set the position of the container to absolute by doing:



                onAdd: function (map) {
                this._div = document.createElement("div");
                this._div.style.padding = "8px";
                this._div.style.border = "2px solid grey";
                this._div.style.borderRadius = "2px";

                // Set "position" to "absolute" manually for "top" and "left"
                // to work. Alternatively, apply CSS styles for leaflet-layer
                // class by adding class for all the necessary styles
                // this._div.style.position = "absolute";

                // See the link below for all the styles included
                // https://github.com/Leaflet/Leaflet/blob/master/dist/leaflet.css
                L.DomUtil.addClass(this._div, "leaflet-layer");

                this.getPane().appendChild(this._div);
                this._update();
                map.on("zoomend viewreset", this._update, this);
                },


                See the link to the leaflet.css for the necessary styles. :D






                share|improve this answer












                For those who might need this piece of information, I have found out the problem. You need to set the position of the container to absolute by doing:



                onAdd: function (map) {
                this._div = document.createElement("div");
                this._div.style.padding = "8px";
                this._div.style.border = "2px solid grey";
                this._div.style.borderRadius = "2px";

                // Set "position" to "absolute" manually for "top" and "left"
                // to work. Alternatively, apply CSS styles for leaflet-layer
                // class by adding class for all the necessary styles
                // this._div.style.position = "absolute";

                // See the link below for all the styles included
                // https://github.com/Leaflet/Leaflet/blob/master/dist/leaflet.css
                L.DomUtil.addClass(this._div, "leaflet-layer");

                this.getPane().appendChild(this._div);
                this._update();
                map.on("zoomend viewreset", this._update, this);
                },


                See the link to the leaflet.css for the necessary styles. :D







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 14 at 7:07









                imckl0117

                112




                112






























                    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.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • 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%2f53256760%2fadding-multiple-leaflet-layers-resulted-in-inaccurately-positioned-containers%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

                    Guess what letter conforming each word

                    Port of Spain

                    Run scheduled task as local user group (not BUILTIN)