“google.maps.MapTypeId is undefined” when using GM

2019-07-07 07:52发布

问题:

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?

回答1:

You can try naming your function that initializes the map and then put it as callback in the link loading Google Maps api.

Something like this:

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

Original answer from here: Google Maps API v3 - TypeError... https://stackoverflow.com/a/8361021/1266136



回答2:

might be a little late, but for me it helped adding this line:

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

before you call mapTypeId: google.maps.MapTypeId.TERRAIN

Like this:

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