TypeError:map is undefined使用谷歌地图API时

时间:2017-05-30 07:59:48

标签: javascript google-maps

我在尝试为我维护的网站实施谷歌地图时遇到了这个问题。我从firebug得到以下错误:

  

TypeError:map未定义

我做错了什么?

注意:我们上周只更改了域名。

谢谢

<div id="mapd" style="width: 100%; height: 550px;"></div>
<script src="https://maps-api-ssl.google.com/maps/api/js?key=mykey&sensor=false" type="text/javascript"></script>
<script type="text/javascript">

var map;
var markers = [];
var markers1 = [];
var infoWindow;


function load1() {

  map = new google.maps.Map(document.getElementById("mapd"), {
    center: new google.maps.LatLng(22.00, 72.22),
    zoom: 8,
    mapTypeId: 'roadmap',
    mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU}
  });
  infoWindow = new google.maps.InfoWindow();


  google.maps.event.addListener(map, 'dragend', function(ev){   
    var s = $("#searchcheck").is(':checked');
    if(s==true)
    {
      $('#loading').css('display','block');
      $('#loading1').css('display','block');
      searchLocationsNear(map.getCenter());
    }
  });

}

1 个答案:

答案 0 :(得分:0)

原因是您尚未在库中添加Google地图API密钥。你可以在图书馆找到关键词'myKey'。

  1. 获取谷歌API密钥Here
  2. 用谷歌提供的密钥替换“myKey”。
  3. 再试一次。
  4. 试用此代码。

    <!DOCTYPE html>
        <html>
          <head>
            <title>Simple Map</title>
            <meta name="viewport" content="initial-scale=1.0">
            <meta charset="utf-8">
        <div id="mapd" style="    width: 100%;
            height: 550px;
            position: absolute;
            z-index: 11;float: inherit;
            display: block;"></div>
    
    
        <script type="text/javascript">
    
        var map;
        var markers = [];
        var markers1 = [];
        var infoWindow;
    
    
        function initMap() {
    
          map = new google.maps.Map(document.getElementById("mapd"), {
            center: new google.maps.LatLng(22.00, 72.22),
            zoom: 8,
            mapTypeId: 'roadmap',
            mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU}
          });
          infoWindow = new google.maps.InfoWindow();
    
    
          google.maps.event.addListener(map, 'dragend', function(ev){   
            var s = $("#searchcheck").is(':checked');
            if(s==true)
            {
              $('#loading').css('display','block');
              $('#loading1').css('display','block');
              searchLocationsNear(map.getCenter());
            }
          });
    
        }
    
        </script>
    
        <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyA1TwYksj1uQg1V_5yPUZqwqYYtUIvidrY&callback=initMap"
            async defer></script>
    
         </body>
        </html>