javascript - How to set a specific position for a leaflet popup - Stack Overflow

I have been doing some mapping with Leaflet and I'm super happy with the result.However, there is

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 badges
Add a ment  | 

1 Answer 1

Reset to default 5

Let 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 no latlng 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745501522a4630438.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信