ReferenceError:刷新页面并删除缓存后未定义Google

时间:2018-09-28 17:37:20

标签: javascript html google-maps google-api

我正在建立一个网站,提供巴士服务,车辆和包裹租赁。有一个名为“ centrales”的部分,您可以在此示例中使用融合的Google Map:
http://jsfiddle.net/maunovaha/jptLfhc8/

放大办公室和中心办公室将要位于的不同位置。相同的坐标。

我遇到的问题是,执行(command + r)时,地图出现没有任何错误。但是,如果我这样做(Shift + Command + R),它不会出现,并且会引发错误ReferenceError:未定义google。这是页面:http://allabordo.borealstudio.mx/centrales.shtml

我在集中页面中的JavaScript如下:

<script>
function initMap(){
  //
}
</script> 
<script src="https://maps.googleapis.com/maps/api/js?key=aquiVaMiLlave&callback=initMap" async defer></script>

还有我在custom.js中进行缩放的那个

var map;
var chicago = new google.maps.LatLng(41.850033, -87.6500523);

function ZoomControl(controlDiv, map) {

controlDiv.style.padding = '5px';
var controlWrapper = document.createElement('div');
controlWrapper.style.backgroundColor = 'white';
controlWrapper.style.borderStyle = 'solid';
controlWrapper.style.borderColor = 'gray';
controlWrapper.style.borderWidth = '1px';
controlWrapper.style.cursor = 'pointer';
controlWrapper.style.textAlign = 'center';
controlWrapper.style.width = '32px'; 
controlWrapper.style.height = '64px';
controlDiv.appendChild(controlWrapper);

var zoomInButton = document.createElement('div');
zoomInButton.style.width = '32px'; 
zoomInButton.style.height = '32px';

zoomInButton.style.backgroundImage = 'url("http://placehold.it  /32/00ff00")';
controlWrapper.appendChild(zoomInButton);

var zoomOutButton = document.createElement('div');
zoomOutButton.style.width = '32px'; 
zoomOutButton.style.height = '32px';

zoomOutButton.style.backgroundImage = 'url("http://placehold.it/32/0000ff")';
controlWrapper.appendChild(zoomOutButton);

google.maps.event.addDomListener(zoomInButton, 'click', function() {
map.setZoom(map.getZoom() + 1);
});

google.maps.event.addDomListener(zoomOutButton, 'click', function() {
map.setZoom(map.getZoom() - 1);
});  

}

function initialize() {
var mapDiv = document.getElementById('map');

var mapOptions = {
zoom: 12,
center: chicago,

disableDefaultUI: true,

}

map = new google.maps.Map(mapDiv, mapOptions);


var zoomControlDiv = document.createElement('div');
var zoomControl = new ZoomControl(zoomControlDiv, map);

 zoomControlDiv.index = 1;                                      
                map.controls[google.maps.ControlPosition.TOP_LEFT].push(zoomControlDiv);
 }

initialize();

1 个答案:

答案 0 :(得分:0)

问题是您正在加载Google Map脚本异步,因此您的脚本在定义Google Map之前就已执行

只需删除异步延迟,然后在包含脚本后放入代码。这应该可以解决问题