首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >ReferenceError:页面刷新和缓存删除时未定义Google

ReferenceError:页面刷新和缓存删除时未定义Google
EN

Stack Overflow用户
提问于 2018-09-29 01:37:20
回答 1查看 542关注 0票数 0

我正在做一个公交车服务,车辆租赁和包裹的网站。有一个名为"centrales“的部分,您可以在其中使用与此示例融合的Google Map:

http://jsfiddle.net/maunovaha/jptLfhc8/

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

我遇到的问题是,当执行(command + r)时,映射出现时没有任何错误。但是如果我这样做了(shift + command + r),它就不会出现,并且会抛出错误ReferenceError: google is not defined。这是页面:http://allabordo.borealstudio.mx/centrales.shtml

我在centrales页面中的javascript如下:

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

我在custom.js中有一个用于缩放的:

代码语言:javascript
复制
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();
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-29 02:49:34

问题是,您正在加载Google Map脚本async,因此您的脚本在定义Google地图之前执行

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52560291

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档