3

Testing the sample code from 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>

on FF 14.0.1, it gives the alert:

TypeError: google.maps.MapTypeId is undefined

on Chrome 16.0.889.0, a div with image shows up.

Why is there such a difference?

4

2 回答 2

2

您可以尝试命名初始化地图的函数,然后将其作为回调放在加载 Google Maps 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>

来自这里的原始答案:Google Maps API v3 - TypeError ... https://stackoverflow.com/a/8361021/1266136

于 2014-06-26T08:12:17.170 回答
0

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

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
                }
              }
              );
于 2012-10-24T11:07:58.320 回答