从HTMLElement获取google.maps.Map实例 [英] Get google.maps.Map instance from a HTMLElement
问题描述
我在页面上有现有的地图。我可以使用document.getElementById()的行来选择该元素以获取HTMLElement JavaScript对象。是否有可能获得地图初始化时创建的google.maps.Map实例,也就是说,它是HTMLElement对象的属性还是原型?
您无法从 DOM元素
获取 google.maps.Map
对象,在其上构建Google地图对象。 google.maps.Map
只是一个包装,它控制 DOM元素
查看地图,而且该元素不包含引用它的包装器。
如果您的问题只是范围,请将 map
作为窗口
对象,并且可以从您网页的任何地方访问它。您可以使用以下其中一种将'map'
设置为全局:
window.map = new google.maps.Map(..)
或
map = new google.maps.Map(...)// AVOID'var'
I have an existing map on a page. I can select that element using something along the lines of document.getElementById() to get the HTMLElement javascript object. Is it possible to get the instance of google.maps.Map created when the map is initialised, i.e. is it a property of the HTMLElement object or in its prototype?
You can't get google.maps.Map
object from DOM Element
,on which Google Maps object have been constructed. google.maps.Map
is just a wrapper, which controls DOM Element
for viewing the map, and that element does not have reference to its wrapper.
If your problem is only the scope, make map
as a property of window
object, and it will be accessible from everywhere in your page. You can make 'map'
as global by using one of these:
window.map = new google.maps.Map(..)
or
map = new google.maps.Map(...) //AVOID 'var'
这篇关于从HTMLElement获取google.maps.Map实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!