“google.maps.MapTypeId is undefined”在FF 14中使用GMAP3时

"google.maps.MapTypeId is undefined" when using GMAP3 in FF 14

本文关键字:GMAP3 FF maps google MapTypeId is undefined      更新时间:2023-09-26

测试来自 gmap3 的示例代码:

<html>    
  <head> 
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>        
    <script src="http://maps.google.com/maps/api/js?sensor=false&language=zh" type="text/javascript"></script>
    <script type="text/javascript" src="js/gmap3.js"></script> 
    <style>
      .gmap3{
        margin: 20px auto;
        border: 1px dashed #C0C0C0;
        width: 500px;
        height: 250px;
      }
    </style>
    <script type="text/javascript">
      $(function(){
        try{
            $('#geoTestDiv').gmap3(
              { action: 'addMarker',
                latLng : [46.578498,2.457275],
                map:{
                  center: true,
                  zoom: 14,
                  mapTypeId: google.maps.MapTypeId.TERRAIN
                }
              }
              );
        }catch(exception){
            alert(exception);
        }
      });
    </script>
  <body>
    <div id="geoTestDiv" class="gmap3"></div>
  </body>
</html>

在 FF 14.0.1 上,它会发出警报:

TypeError: google.maps.MapTypeId 未定义

在 Chrome 16.0.889.0 上,会显示一个带有图像的div。

为什么会有这样的差异?

您可以尝试命名初始化地图的函数,然后将其作为回调放入加载 Google 地图 API 的链接中。

像这样:

<script src="http://maps.google.com/maps/api/js?sensor=false&language=zh&callback=initMainMap" type="text/javascript"></script>
<script type="text/javascript">
   function initMainMap(){
      //function body
   }
</script>

原始答案来自这里: 谷歌地图API v3 - 类型错误...https://stackoverflow.com/a/8361021/1266136

可能

有点晚了,但对我来说,它有助于添加这一行:

var myLatlng = new google.maps.LatLng(0.0, 0.0);

在致电mapTypeId: google.maps.MapTypeId.TERRAIN之前

喜欢这个:

 var myLatlng = new google.maps.LatLng(0.0, 0.0); // this will somehow initialize google.maps...
 $('#geoTestDiv').gmap3(
              { action: 'addMarker',
                latLng : [46.578498,2.457275],
                map:{
                  center: true,
                  zoom: 14,
                  mapTypeId: google.maps.MapTypeId.TERRAIN
                }
              }
              );