admin管理员组文章数量:1022560
I have been doing some mapping with Leaflet and I'm super happy with the result.
However, there is just one little thing that has been bothering me:
I have got two different circles, with popups bound to them, and depending on where I click on the circle, the popup opens in a different location...
Here is some super simple example code.
const circle = new L.circle( [52.5, 13.35] ).addTo(map);
var popup = L.popup().setContent("hello");
I have also tried var popup = L.popup().setLatLng( [52.5, 13.35] ).setContent("hello");
So is there a (preferably) easy way to make the popup box open at the middle of the circle (or have the middle of the circle as the 'anchor point'), so that where ever I click on the circle the popup always opens at the same place?
Very minor, but would appreciate any help, or directions towards a library,
Thanks!
I have been doing some mapping with Leaflet and I'm super happy with the result.
However, there is just one little thing that has been bothering me:
I have got two different circles, with popups bound to them, and depending on where I click on the circle, the popup opens in a different location...
Here is some super simple example code.
const circle = new L.circle( [52.5, 13.35] ).addTo(map);
var popup = L.popup().setContent("hello");
I have also tried var popup = L.popup().setLatLng( [52.5, 13.35] ).setContent("hello");
So is there a (preferably) easy way to make the popup box open at the middle of the circle (or have the middle of the circle as the 'anchor point'), so that where ever I click on the circle the popup always opens at the same place?
Very minor, but would appreciate any help, or directions towards a library,
Thanks!
Share Improve this question asked Jun 19, 2020 at 10:43 DiegovoDiegovo 752 silver badges7 bronze badges1 Answer
Reset to default 5Let me quote the Leaflet documentation about the openPopup
method that every L.Layer
(including every L.Circle
) has:
openPopup(<LatLng> latlng?)
Opens the bound popup at the specified
latlng
or at the default popup anchor if nolatlng
is passed.
Therefore you can:
const circle = new L.circle( [52.5, 13.35] ).bindPopup("hello").addTo(map);
circle.openPopup([52.5, 13.35]);
or
const circle = new L.circle( [52.5, 13.35] ).bindPopup("hello").addTo(map);
circle.openPopup(circle.getLatLng());
or
const circle = new L.circle( [52.5, 13.35] ).bindPopup("hello").addTo(map);
circle.on('click', function(ev) { circle.openPopup(circle.getLatLng()) });
or even
const circle = new L.circle( [52.5, 13.35] ).bindPopup("hello").addTo(map);
circle.on('click', function(ev) { ev.target.openPopup(ev.target.getLatLng()) });
See a working example.
I have been doing some mapping with Leaflet and I'm super happy with the result.
However, there is just one little thing that has been bothering me:
I have got two different circles, with popups bound to them, and depending on where I click on the circle, the popup opens in a different location...
Here is some super simple example code.
const circle = new L.circle( [52.5, 13.35] ).addTo(map);
var popup = L.popup().setContent("hello");
I have also tried var popup = L.popup().setLatLng( [52.5, 13.35] ).setContent("hello");
So is there a (preferably) easy way to make the popup box open at the middle of the circle (or have the middle of the circle as the 'anchor point'), so that where ever I click on the circle the popup always opens at the same place?
Very minor, but would appreciate any help, or directions towards a library,
Thanks!
I have been doing some mapping with Leaflet and I'm super happy with the result.
However, there is just one little thing that has been bothering me:
I have got two different circles, with popups bound to them, and depending on where I click on the circle, the popup opens in a different location...
Here is some super simple example code.
const circle = new L.circle( [52.5, 13.35] ).addTo(map);
var popup = L.popup().setContent("hello");
I have also tried var popup = L.popup().setLatLng( [52.5, 13.35] ).setContent("hello");
So is there a (preferably) easy way to make the popup box open at the middle of the circle (or have the middle of the circle as the 'anchor point'), so that where ever I click on the circle the popup always opens at the same place?
Very minor, but would appreciate any help, or directions towards a library,
Thanks!
Share Improve this question asked Jun 19, 2020 at 10:43 DiegovoDiegovo 752 silver badges7 bronze badges1 Answer
Reset to default 5Let me quote the Leaflet documentation about the openPopup
method that every L.Layer
(including every L.Circle
) has:
openPopup(<LatLng> latlng?)
Opens the bound popup at the specified
latlng
or at the default popup anchor if nolatlng
is passed.
Therefore you can:
const circle = new L.circle( [52.5, 13.35] ).bindPopup("hello").addTo(map);
circle.openPopup([52.5, 13.35]);
or
const circle = new L.circle( [52.5, 13.35] ).bindPopup("hello").addTo(map);
circle.openPopup(circle.getLatLng());
or
const circle = new L.circle( [52.5, 13.35] ).bindPopup("hello").addTo(map);
circle.on('click', function(ev) { circle.openPopup(circle.getLatLng()) });
or even
const circle = new L.circle( [52.5, 13.35] ).bindPopup("hello").addTo(map);
circle.on('click', function(ev) { ev.target.openPopup(ev.target.getLatLng()) });
See a working example.
本文标签: javascriptHow to set a specific position for a leaflet popupStack Overflow
版权声明:本文标题:javascript - How to set a specific position for a leaflet popup - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745501521a2153414.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论