Javascript 无法让谷歌地图与requirejs一起使用
我是一个新的需要js的人,我已经尝试了所有可能在谷歌上找到的关于谷歌地图和需要js的东西。在我的页面上,除了谷歌地图,其他一切都正常。如果我刷新页面,谷歌地图显示良好,但在浏览页面时不会显示 这是我的main.js }) 这就是我的gmap.js的样子Javascript 无法让谷歌地图与requirejs一起使用,javascript,php,google-maps,requirejs,semantic-ui,Javascript,Php,Google Maps,Requirejs,Semantic Ui,我是一个新的需要js的人,我已经尝试了所有可能在谷歌上找到的关于谷歌地图和需要js的东西。在我的页面上,除了谷歌地图,其他一切都正常。如果我刷新页面,谷歌地图显示良好,但在浏览页面时不会显示 这是我的main.js }) 这就是我的gmap.js的样子 define('gmap',['async!https://maps.googleapis.com/maps/api/js?key=API_KEY'],function(){ return window.google.maps;}) 除了req
define('gmap',['async!https://maps.googleapis.com/maps/api/js?key=API_KEY'],function(){
return window.google.maps;})
除了requirejs本身,页面上没有其他脚本文件使用基本脚本标记,如
<script data-main="scripts/main" type="text/JavaScript" src="scripts/require.js"></script>
注意:我使用的是语义用户界面,如果这有什么区别的话
任何建议都将不胜感激 google.maps.event.addDomainListenerWindow,“加载”,initGMap;是我的罪魁祸首。即使在页面加载完成后,也不会触发事件“load”,因此我在$document.ready内调用initGMap而不是使用此语句
此案例的完整解决方案
新gmap.js
main.js
我有一种感觉,需要一个gmap的嵌套定义。如果你把console.loggmap作为你需求的第一行,它是作为未定义的还是你得到了这个对象?它显示未定义的,但不是每次我加载,几乎每一次交替加载
<script data-main="scripts/main" type="text/JavaScript" src="scripts/require.js"></script>
define('gmap',['async!https://maps.googleapis.com/maps/api/js?key=API_KEY'],function(){
self.mapStart = function(){
console.log('Initiating google maps');
var myMap = new google.maps.Map(document.getElementById('gmap-canvas'),{
center: new google.maps.LatLng(Info.lat, Info.lon),
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow({
content: Info.contStr
});
var marker = new google.maps.Marker({
position: new google.maps.LatLng(Info.lat, Info.lon),
map: myMap,
title: 'My title',
});
infowindow.open(myMap, marker);
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(myMap,marker);
});
}
return self;
})
require(['gmap','jquery','semantic'],function(gmap,$,sm){
$(document).ready(function(){
gmap.mapStart();
});
});