Does a Google Map element have to be in the DOM when instantiating?

无人久伴 提交于 2019-12-24 02:57:06

问题


I am instantiating a Google Map object like so:

this.map = new google.maps.Map(this.el, {
    center: new google.maps.LatLng(this.lat, this.lng),
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    zoom: this.zoom
});

Where this.el is an object created via document.createElement('div') but has not yet been inserted into the DOM. When I later append this.el to the DOM I get a map that looks like this (note all the weird gray space):

http://cl.ly/3B3z1e3g2h1U301r0X1R

I do not have this problem, though, if I first append this.el to the DOM and then instantiate the map object. Is there a way to create the map and then append it? This would greatly simplify some of my map code (I am using this in Backbone.js views).

Thanks.

-Scott


回答1:


After you append this.el to the DOM, you have to call google.maps.event.trigger(this.map, 'resize');



来源:https://stackoverflow.com/questions/9609543/does-a-google-map-element-have-to-be-in-the-dom-when-instantiating

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!