【问题标题】:"google is not defined" error calling google maps api in Chrome在 Chrome 中调用谷歌地图 API 时出现“未定义谷歌”错误
【发布时间】:2016-12-30 01:14:04
【问题描述】:

我开始学习谷歌地图 API。该页面正在离线工作,但它在带有 Chrome 的服务器上显示“谷歌未定义”。当我使用火狐时,没有错误。

这里是html:

<script src="https://maps.googleapis.com/maps/api/js?key=MY_API_KEY&libraries=places&callback=initAutocomplete" async defer></script>

这里是javascript:

    var initCenter;
    var map;
    var cityCircle;
    var centerLatLng;
    var boundsLatLng;
    var distanceBtw;
    var citymap;
    var lati,longi ;
    var path = window.location.pathname;
    var webCtx = path.substring(0, path.indexOf('/', 1));
    var pointImg= webCtx + '/front-end/ad/image/PointRed.png';

function initAutocomplete() {

   map = new google.maps.Map(document.getElementById('adMap'), {
   zoom: 10,
   zoomControl: true,
   mapTypeControl: false,
   scaleControl: true,
   streetViewControl: false,
   mapTypeId: google.maps.MapTypeId.ROADMAP
   });

   var input = document.getElementById('pac-input');
   var searchBox = new google.maps.places.SearchBox(input);
   map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);

   map.addListener('bounds_changed', function() {
        searchBox.setBounds(map.getBounds());
    });

       map.addListener('click',function(e){
            marker.setMap(null),
            marker=null,
            marker = new google.maps.Marker({
            position: e.latLng,
            map: map,
            icon:pointImg,
            });

            cityCircle.setCenter(e.latLng);
            boundsLatLng=cityCircle.getBounds();
            map.fitBounds(boundsLatLng);
            }); 

     citymap = {
       chungyang: {
       population: 1000000
       }
     };

   for (var city in citymap) {

       cityCircle = new google.maps.Circle({
       strokeColor: '#F2A57E',
       strokeOpacity: 0.8,
       strokeWeight: 2,
       fillColor: '#F2A57E',
       fillOpacity: 0.35,
       map: map,
       center: citymap[city].center,
       radius: Math.sqrt(citymap[city].population) * adRange
     });

         cityCircle.addListener('click',function(e){
             marker.setMap(null),
             marker=null,
             marker = new google.maps.Marker({
             position: e.latLng,
             map: map,
             icon:pointImg,
             draggable: true,
             });
             cityCircle.setCenter(e.latLng);
             boundsLatLng=cityCircle.getBounds();
             map.fitBounds(boundsLatLng);
             })
          }

        searchBox.addListener('places_changed', function() {

        var places = searchBox.getPlaces();

           if (places.length == 0) {
             return;
           }

          var bounds = new google.maps.LatLngBounds();
          places.forEach(function(place) {

               if (place.geometry.viewport) {
               // Only geocodes have viewport.
                   bounds.union(place.geometry.viewport);
               } else {
                   bounds.extend(place.geometry.location);
               }
          });

           map.fitBounds(bounds);

        }  //searchBox.addListener

   if (navigator.geolocation) {

         navigator.geolocation.getCurrentPosition(succCallback,errCallback,{
             enableHighAccuracy:false,
             timeout:10000,
             maximumAge:0
         });

     } else {
         // alert('not support geolocation');
     }
 }

   function succCallback(e) {
   initCenter = new google.maps.LatLng(e.coords.latitude,e.coords.longitude);
     map.setCenter(initCenter);
         cityCircle.setCenter(initCenter);
         lati = e.coords.latitude;
         longi = e.coords.longitude;

                 marker = new google.maps.Marker({
                 map: map,
                 icon:pointImg,
                 position:initCenter
                 });


          $('#ad_center').val('(' + lati + ',' + longi+')');
          $('#ad_centerLati').val(lati);
          $('#ad_centerLongi').val(longi);
      }

     function errCallback(e){
       initCenter = new google.maps.LatLng(30.09108, 130.5598);
         map.setCenter(initCenter);
         cityCircle.setCenter(initCenter);

               marker = new google.maps.Marker({
               map: map,
               icon:pointImg,
               position:initCenter
               });

     }

【问题讨论】:

  • 你是不是在你的js文件开头初始化了一些变量?
  • 将脚本粘贴到

标签: javascript google-maps jsp google-chrome


【解决方案1】:

根据这个thread,检查是否有可能导致问题的插件。

还说:

在您的页面&lt;script src="https://maps.googleapis.com/maps/api‌​/js" async defer&gt;&lt;/script&gt; 上包含 GMap 的新方法会导致问题,因为它的加载将被延迟,以免成为阻塞资源。这有在初始化 GMaps 之前包含其他脚本的尴尬可能性。

试试这个:

<script src="http://maps.googleapis.com/maps/api/js"></script> 

来源:https://github.com/googlemaps/v3-utility-library/blob/master/canvaslayer/examples/hello2d.html

【讨论】:

  • 嗨,我尝试了以下两种方式 1.&lt;script src="http://maps.googleapis.com/maps/api/js?sensor=false"&gt;&lt;/script&gt; 错误消息:“Google Maps API 警告:NoApiKeys”和“Google Maps API 错误:MissingKeyMapError”2.&lt;script src="https://maps.googleapis.com/maps/api/js?key=MY_API_KEY"&gt;&lt;/script&gt; 错误消息:未捕获TypeError: Cannot read property 'SearchBox' of undefined 感谢您的回复!
  • 对我来说无论哪种方式都没有区别。 Jut 同样的错误-> EXCEPTION: Error in :0:0 cause: google is not defined 似乎项目由于某种原因根本没有读取脚本。
猜你喜欢
  • 1970-01-01
  • 2020-09-20
  • 2023-03-06
  • 1970-01-01
  • 2016-05-09
  • 1970-01-01
  • 1970-01-01
  • 2015-12-24
  • 2014-09-02
相关资源
最近更新 更多