谷歌地图API捕获列表无法读取属性'offsetwidth '零

hims056jaeyong提出了一个问题:Google MAP API Uncaught TypeError: Cannot read property 'offsetWidth' of null,或许与您遇到的问题类似。

回答者Communitygeocodezip给出了该问题的处理方式:

This problem is usually due to the map div not being rendered before the javascript runs that needs to access it.

You should put your initialization code inside an onload function or at the bottom of your HTML file, just before the tag, so the DOM is completely rendered before it executes (note that the second option is more sensitive to invalid HTML).

Note, as pointed out by matthewsheetsthis also could be cause by the div with that id not existing at all in your HTML (the pathological case of the div not being rendered)

Adding code sample from wf9a5m75's post to put everything in one place:

<script type="text/javascript">

function initialize() {
    var latlng = new google.maps.LatLng(-34.397, 150.644);
    var myOptions = {
        zoom: 8,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("map_canvas"),
            myOptions);
}
google.maps.event.addDomListener(window, "load", initialize);

</script>

希望本文对你有帮助,欢迎支持JavaScript中文网

原文链接:stackoverflow.com

上一篇:OnChange事件输入类型=范围不触发在Firefox中拖动时
下一篇:Onclick JavaScript使浏览器返回上一页?

相关推荐

官方社区

扫码加入 JavaScript 社区