javascript - 无法获取属性值 'offsetWidth' : object is null or undefined error

标签 javascript google-maps google-maps-api-3

我正在开发一个基本上被设置为三行表格的页面。顶行是标题,中间行是页面的主要部分,底行是导航。导航行中的每个按钮都会触发一个 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/

相关文章:

javascript - Google Maps API V3 读取 KML 文件

javascript - 谷歌地图 API : Remove and reset markers on mouseover

google-maps - 如何使用谷歌地图将地址地理编码为纬度/经度

javascript - 如何将 Yii2 与 require.js 一起使用?

javascript - Nunjucks 模板中的变量解构

javascript - app.directive Google map 在托管后无法工作

java - Eclipse Java Android - 如果没有 Google Play 服务,该应用程序将无法运行

Google Maps Services Places 的 Python 客户端无法传递 Page_Token

javascript - 为什么有时使用点表示法访问对象属性会返回未定义?

javascript - 使用VelocityJS解析VTL时如何修复 "Unexpected token $ in JSON"