问题描述
我有一个浏览器插件的想法,该插件可以操纵第三方站点上的Leaflet地图.但是,我只停留在一个简单的事情上:我不确定如何在页面上发现Leaflet映射DOM元素,然后访问关联的Leaflet map
对象.
I have an idea for a browser plugin which would manipulate Leaflet maps on third-party sites. But I'm stuck on a simple thing: I'm not sure how to discover Leaflet map DOM elements on a page, and then access the associated Leaflet map
object.
-
$('.leaflet-container')
是查找所有地图对象的可靠方法吗?
Is
$('.leaflet-container')
a reliable way to find all map objects?
如何从该对象实际访问 map
对象,因此我可以执行以下操作: $('.leaflet-container').eachLayer(...)
,这是行不通的.
How to actually access the map
object from that object, so I can do something like: $('.leaflet-container').eachLayer(...)
, which doesn't work.
此问题与,但是答案是有一种无法解决的变通方法.
This question is essentially the same as How can I get a leaflet.js instance using only a DOM object?, but the answer there was a workaround which doesn't help.
推荐答案
- 是的,这已经足够了,尽管这不是记录在案的行为,并且至少在理论上会在Leaflet的未来版本中发生变化
- 无法执行此操作.对该地图的引用归创建该地图的代码所有,它可能已将其丢弃或可能将其存储在您无权访问的位置.Leaflet本身不会在您可以访问的任何地方存储对地图的引用
从侧面讲,我认为您应该让代码的用户明确地将引用传递给您,而不是尝试自动查找引用.例如,参见控制原理的反转,其中调用方提供了依赖项;《德米特律法》 在某种程度上也适用-请勿窥探其他代码内部结构(除非您确实如此,真的必须).
On a side note, it is my opinion that you should rather let users of your code explicitly pass references to you, rather than you trying to automatically find references. See for example the inversion of control principle, where the caller supplies dependencies; Law of Demeter is also somewhat applicable - don't pry into other codes internals (unless you really, really have to).
这篇关于查找页面上存在的Leaflet映射对象,没有变量引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!