javascript - 谷歌未定义错误

标签 javascript html

我正在使用 Google Maps API 制作网页,但收到“google 未定义错误”

如何摆脱这个问题?

如何导入 Google 或执行某些操作以使这段代码正常工作?

我想要一个程序,用户可以在其中输入位置并在那里显示标记。但它无法正常工作。

这是我的代码:

 <!DOCTYPE html>
    <html>
      <head>
        <title>Simple Map</title>
        <meta name="viewport" content="initial-scale=1.0">
        <meta charset="utf-8">
        <style>
          html, body {
          height: 100%;
          margin: 0;
          padding: 0;
          }
          #map {
          height: 100%;
          }
        </style>
      </head>
      <body>
        <h1>Perform Google Maps Search</h1>
        <h3> Please enter the place you want to search</h3>
        <input type="text" id="mapsearch" size="50"> <br>
        <br>
        <div id="map"></div>
        <script>
          var map;
          function initMap() {
          
          var myOptions = { 
                     zoom:14,    
                    navigationControl: true,     
                    scaleControl: true,
                     panControl: true,
                     center: new google.maps.LatLng(43.6532,-79.3832),
                     mapTypeId: google.maps.MapTypeId.ROADMAP
                    } 
          
            map = new google.maps.Map(document.getElementById('map'),myOptions);
          
          var marker = new google.maps.Marker({
          position: new google.maps.LatLng(43.6532,-79.3832),
          title:"Toronto"
          });
          marker.setMap(map);
          }
          
          var searchBox = new google.maps.places.SearchBox(document.getElementById('mapsearch'));
          map.controls[google.maps.ControlPosition.TOP_CENTER].push(document.getElementById('mapsearch'));
          google.maps.event.addListener(searchBox, 'places_changed', function() {
          searchBox.set('map', null);
          
          
          var places = searchBox.getPlaces();
          
          var bounds = new google.maps.LatLngBounds();
          var i, place;
          for (i = 0; place = places[i]; i++) {
           (function(place) {
             var marker = new google.maps.Marker({
          
               position: place.geometry.location
             });
             marker.bindTo('map', searchBox, 'map');
             google.maps.event.addListener(marker, 'map_changed', function() {
               if (!this.getMap()) {
                 this.unbindAll();
               }
             });
             bounds.extend(place.geometry.location);
          
          
           }(place));
          
          }
          map.fitBounds(bounds);
          searchBox.set('map', map);
          map.setZoom(Math.min(map.getZoom(),12));
          
          });
          google.maps.event.addDomListener(window, 'load', init);
        </script>
        <script src="https://maps.googleapis.com/maps/api/js?key="Your_API_Key"&callback=initMap"
          async defer></script>
      </body>
    </html>

最佳答案

initMap 函数应该在您关闭它之前最后关闭 var searchBox = new google.maps.places.SearchBox(document.getElementById('mapsearch'));

所以你会收到谷歌未定义的错误。

您还添加了参数 &libraries=places 到 google map 脚本 src

工作示例

https://plnkr.co/edit/ngtGvuhDDZAnovwPnPXh?p=preview

// Code goes here

 var map;
      function initMap() {

    var myOptions = { 
                 zoom:14,    
                navigationControl: true,     
                scaleControl: true,
                 panControl: true,
                 center: new google.maps.LatLng(43.6532,-79.3832),
                 mapTypeId: google.maps.MapTypeId.ROADMAP
                } 

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

        var marker = new google.maps.Marker({
            position: new google.maps.LatLng(43.6532,-79.3832),
            title:"Toronto"
        });
    
      marker.setMap(map);
      var searchBox = new google.maps.places.SearchBox(document.getElementById('mapsearch'));
   map.controls[google.maps.ControlPosition.TOP_CENTER].push(document.getElementById('mapsearch'));
   google.maps.event.addListener(searchBox, 'places_changed', function() {
     searchBox.set('map', null);


     var places = searchBox.getPlaces();

     var bounds = new google.maps.LatLngBounds();
     var i, place;
     for (i = 0; place = places[i]; i++) {
       (function(place) {
         var marker = new google.maps.Marker({

           position: place.geometry.location
         });
         marker.bindTo('map', searchBox, 'map');
         google.maps.event.addListener(marker, 'map_changed', function() {
           if (!this.getMap()) {
             this.unbindAll();
           }
         });
         bounds.extend(place.geometry.location);


       }(place));

     }
     map.fitBounds(bounds);
     searchBox.set('map', map);
     map.setZoom(Math.min(map.getZoom(),12));

   });

      }
<!DOCTYPE html>
<html>
  <head>
    <title>Simple Map</title>
    <meta name="viewport" content="initial-scale=1.0">
    <meta charset="utf-8">
    <style>
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
      #map {
        height: 100%;
      }
    </style>
  </head>
  <body>
  <h1>Perform Google Maps Search</h1>
  <h3> Please enter the place you want to search</h3>
  <input type="text" id="mapsearch" size="50"> <br>
  <br>
    <div id="map"></div>
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAtaKPvRV8-ciYtnnzG3QI3CO7m4HJyhaI&libraries=places&callback=initMap"
    async defer></script>
    
  </body>
</html>

关于javascript - 谷歌未定义错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48859418/

相关文章:

javascript - 如何通过 jQuery 获取 html 中具有特定类的第一个元素的 ID 值

html - 更改列中的输入大小

javascript - 在 nodejs 中执行 128 位数学运算

javascript - 将多个选择器传递给一个函数

javascript - 可以提交两个表格吗?网络MVC

php - 遍历 WordPress 中的类别

html - 将另一个类中的 div 类对齐到底部

javascript - Google Maps API 启用 ScrollWheelZoom() - 为什么它不能正确缩小?

javascript - 排版的某些部分(字母)响应窗口的宽度/高度?

javascript - A* 寻路实现错误导致死循环