【问题标题】:Google Maps Uncaught TypeError: Cannot read property 'LatLng' of undefinedGoogle Maps Uncaught TypeError:无法读取未定义的属性“LatLng”
【发布时间】:2012-12-06 14:27:56
【问题描述】:

我不断收到此错误“未捕获的类型错误:无法读取未定义的属性 'LatLng'” 尝试创建地图时
在头脑中:

 <script type="text/javascript" src="https://www.google.com/jsapi"></script>
 <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.js"/>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3&sensor=true"></script>

......

function init(){


            var latlng = new google.maps.LatLng(-43.552965, 172.47315);
            var myOptions = {
                zoom: 10,
               center: latlng,
               mapTypeId: google.maps.MapTypeId.ROADMAP

            };
           map = new google.maps.Map(document.getElementById("map"), myOptions);


        }

......

    <body>
        <div id="map" style="width:600px;height: 600px;">
        </div>
        <ul class="navigation">
        </ul>
    </body>

【问题讨论】:

    标签: javascript api google-maps


    【解决方案1】:

    如果一起使用,上述两个答案将解决此问题。属性 LatLng 未定义,因为 google 对象尚不可用。

    您总是关闭您的&lt;script&gt; 标签期间。

    google 对象在 DOM 加载之前不可用。所以在你的javascript中你必须使用谷歌地图的addDomListener()。 Kara 的解决方案是正确的,但它不适用于您的情况,因为函数名称是 init 并且 addDomListener 必须等待“加载”。你需要:

    google.maps.event.addDomListener(window, 'load', init);
    

    另一个非常简单的解决方案是将回调添加到脚本中

    src="https://maps.googleapis.com/maps/api/js?v=3&sensor=true&callback=init
    

    回调将等待脚本加载,然后触发您的 init 函数来初始化和绘制您的地图。

    我把解决方案放在 CodePen 这里http://codepen.io/redbirdisu/pen/BHivq

    【讨论】:

      【解决方案2】:

      看起来问题是缺少 jquery.js 包含的 &lt;script&gt; 的结束标记:

       <script 
         type="text/javascript"
         src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.js"/>
       <script 
         type="text/javascript" 
         src="https://maps.googleapis.com/maps/api/js?v=3&sensor=true">
       </script>
      

      &lt;script&gt;标签需要用&lt;/script&gt;封闭,应该是:

       <script 
         type="text/javascript" 
         src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.js">     
       </script>
       <script 
         type="text/javascript" 
         src="https://maps.googleapis.com/maps/api/js?v=3&sensor=true">
       </script>
      

      欲了解更多信息,请参阅:Why don't self-closing script tags work?

      【讨论】:

        【解决方案3】:

        你什么时候运行你的初始化函数? 如果 init 函数在 google.js 加载之前运行,你会得到这个错误。

        尝试像这样调用初始化函数。

        function init(){
              //...
        }
        google.maps.event.addDomListener(window, 'init', Initialize);
        

        【讨论】:

          【解决方案4】:

          我也有同样的问题,所有语法都是正确的。尝试通过将脚本文件向上/向下移动 HTML 文件来修复,但没有一个成功。

          我所做的是,从 URL 中删除 &amp;callback=init 并在 $(document).ready(function () {}) 中调用该方法。现在它完美地工作了。

          【讨论】:

            猜你喜欢
            • 2016-09-10
            • 1970-01-01
            • 2013-12-16
            • 1970-01-01
            • 2021-04-28
            • 2017-07-20
            • 1970-01-01
            • 2020-03-28
            • 2023-04-11
            相关资源
            最近更新 更多