Get google.maps.Map instance from a HTMLElement

后端 未结 5 654
故里飘歌
故里飘歌 2021-01-31 07:21

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

5条回答
  •  没有蜡笔的小新
    2021-01-31 08:21

    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' 
    

提交回复
热议问题