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!
javascript leaflet
add a comment |
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!
javascript leaflet
add a comment |
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!
javascript leaflet
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
javascript leaflet
edited Nov 12 at 6:17
Foo
1
1
asked Nov 12 at 6:15
imckl0117
112
112
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 14 at 7:07
imckl0117
112
112
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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