Get google.maps.Map instance from a HTMLElement

后端 未结 5 613
故里飘歌
故里飘歌 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:16

    You create an instance when you initialize a map;

    var map = new google.maps.Map(document.getElementById("map_element"), options);
    

    You use that instance whenever you want to do something like putting a marker, changing location, etc. It's not an HTMLElement object. However it has a getDiv() method, which gives you the html element it's operating on.

    map.getDiv(); // in this case it returns the element with the id 'map_element'
    

提交回复
热议问题