传单:将同一图层添加到两个不同的地图中 [英] Leaflet: add same layer to two different maps
问题描述
我在同一页面中显示两个不同的 leaflet
map
.在某个事件之后.我希望 Polyline
出现在它们两个上.如果可能的话,我想避免创建一个副本并保留两个不同的变量.
I am displaying two different leaflet
map
s in the same page. After a certain event. I want a Polyline
to appear on both of them. I would like to avoid, if possible, to create a copy and keep two different variables.
我正在尝试使用以下内容:
I am trying yo use the following:
var line_coordinates = [[1,2],[3,4]];
var my_polyline = L.polyline(line_coordinates);
my_polyline.addTo(map1);
my_polyline.addTo(map2);
但是,如果我运行上述代码,则 Polyline
将仅显示在 map2
上.
However, if I run the above code, the Polyline
will be displayed only on the map2
.
后记,我将需要再次将其坐标更改为某些 new_line_coordinates
,然后运行以下代码:
Afterwords, I will need to change again its coordinates to some new_line_coordinates
, and I will run the following:
my_polyline.setLatLngs(new_line_coordinates);
my_polyline.redraw();
折线
现在应显示为已更新为新坐标.但是,它再次仅出现在 map2
上.
The polyline
should now appear updated to the new coordinates. However, again, it appears only on map2
.
我做错了什么?有可能实现我想做的事情?
What am I doing wrong? Is it possible to achieve what I am trying to do?
推荐答案
正如注释中提到的geocodezip一样,在地图上添加折线会设置折线对象的 this._map
实例变量.因此,无法以当前实现的方式在两个地图上呈现该对象.
As geocodezip mentioned in a comment, adding a polyline to a map sets the polyline object's this._map
instance variable. Therefore, there is no way to have the object rendered on both maps with the way in which it is currently implemented.
您可以查看相关的源代码这里.
You can view the relevant source code here.
这篇关于传单:将同一图层添加到两个不同的地图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!