我正在开发一个基本上被设置为三行表格的页面。顶行是标题,中间行是页面的主要部分,底行是导航。导航行中的每个按钮都会触发一个 javascript 函数,该函数使用中间行的 innerHTML 值并放置必要的 html 以显示页面内容。 在我的主页上:
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=MY_KEY&sensor=false"></script>
在头部我有:
var myOptions = {center: new google.maps.LatLng(-34.397, 150.644), zoom: 8, mapTypeId: google.maps.MapTypeId.ROADMAP };
在 body 部分,以便它立即运行,然后在由 MAP 按钮调用的函数中,我有:
var mapString;
var w = document.getElementById("mainwindow");
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
mapString = '<table width="1024" cellpadding="0" cellspacing="0"><tr>';
mapString += '<td> <div id="map_canvas" width="1024" height="350"></div></td>';
mapString += '</tr></table>';
w.innerHTML = mapString;
现在,当我按下 map 按钮时,出现错误:
Unable to get value of the property 'offsetWidth': object is null or undefined
main.js Line 29
Code 0 char:1385
URI: http://maps.gstatic.com/intl/en_us/mapfiles/api-2/3/2/main.js
不确定这个错误试图告诉我什么。任何帮助将不胜感激。
最佳答案
做
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
最后是因为 map_canvas
div 在 google maps 尝试将 map 加载到该 div 时不存在。
应该这样读
var mapString;
var w = document.getElementById("mainwindow");
mapString = '<table width="1024" cellpadding="0" cellspacing="0"><tr>';
mapString += '<td> <div id="map_canvas" width="1024" height="350"></div></td>';
mapString += '</tr></table>';
w.innerHTML = mapString;
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
关于javascript - 无法获取属性值 'offsetWidth' : object is null or undefined error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9747557/