Google MAP API 未捕获类型错误:无法读取 null 的属性“offsetWidth" [英] Google MAP API Uncaught TypeError: Cannot read property 'offsetWidth' of null

查看:42
本文介绍了Google MAP API 未捕获类型错误:无法读取 null 的属性“offsetWidth"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过以下代码使用 Google MAP API v3.

拓扑

<script src="https://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script><link rel="stylesheet" type="text/css" href="{% url css_media 'tooltip.topology.css' %}"/><link rel="stylesheet" type="text/css" href="{% url css_media 'tooltip.css' %}"/><style type="text/css" >#map_canvas {宽度:300px;高度:300px;}</风格><script type="text/javascript">var latlng = new google.maps.LatLng(-34.397, 150.644);var myOptions = {变焦:8,中心:latlng,mapTypeId: google.maps.MapTypeId.ROADMAP};var map = new google.maps.Map(document.getElementById("map_canvas"),我的选项);<div id="map_canvas">

当我运行这段代码时,浏览器会提示这个.

<块引用>

未捕获的类型错误:无法读取 null 的属性 'offsetWidth'

我不知道,因为我遵循了本教程中给出的指导.

你有什么线索吗?

解决方案

这个问题通常是由于地图 div 在需要访问它的 javascript 运行之前没有被渲染.

您应该将初始化代码放在 onload 函数内或 HTML 文件底部,就在标记之前,以便 DOM 在执行之前完全呈现(请注意,第二个选项对无效 HTML 更敏感).

请注意,正如 matthewsheets 所指出的,这也可能是由于 div 中根本不存在该 id您的 HTML(未呈现 div 的病理情况)

wf9a5m75 的帖子中添加代码示例以将所有内容放在一个地方:

I'm trying to use Google MAP API v3 with the following code.

<h2>Topology</h2>

<script src="https://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script>

<link rel="stylesheet" type="text/css" href="{% url css_media 'tooltip.topology.css' %}" />
<link rel="stylesheet" type="text/css" href="{% url css_media 'tooltip.css' %}" />

<style type="text/css" >
      #map_canvas {
              width:300px;
            height:300px;
     }
</style>

<script type="text/javascript">

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);

</script>

<div id="map_canvas"> </div>

When I run this code, the browser says this.

Uncaught TypeError: Cannot read property 'offsetWidth' of null

I have no idea, since I follow the direction given in this tutorial.

Do you have any clue?

解决方案

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 matthewsheets this 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>

这篇关于Google MAP API 未捕获类型错误:无法读取 null 的属性“offsetWidth"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆