我无法使用 GoogleMaps API V3 运行我的 map 。 map 不加载。我希望 map 出现在 ID 为 gisMap
的 div 中,但在 Chrome 调试器中我收到消息:
Uncaught InvalidValueError: initMap is not a function
Javascript
var map;
function initMap() {
// Enabling new cartography and themes
google.maps.visualRefresh = true;
// Setting starting options
var mapOptions = {
center: new google.maps.LatLng(39.9078, 32.8252),
zoom: 10,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
// Getting Map DOM Element
var mapElement = document.getElementById('gisMap');
// Creating a map with DOM element
map = new google.maps.Map(mapElement, mapOptions);
}
Bundle.js(节选)
(...)
module.exports = Vue;
}).call(this,require('_process'))
},{"_process":1}],3:[function(require,module,exports){
'use strict';
var map;
function initMap() {
// Enabling new cartography and themes
google.maps.visualRefresh = true;
// Setting starting options
var mapOptions = {
center: new google.maps.LatLng(39.9078, 32.8252),
zoom: 10,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
// Getting Map DOM Element
var mapElement = document.getElementById('gisMap');
// Creating a map with DOM element
map = new google.maps.Map(mapElement, mapOptions);
}
},{}],4:[function(require,module,exports){
'use strict';
var Vue = require('vue');
new Vue({});
(...)
HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>MFServer Test</title>
<link rel="stylesheet" href="/css/app.css">
</head>
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">MFDispo</a>
</div>
<div id="navbar" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Start</a></li>
<li><a href="#about">GIS</a></li>
</ul>
</div><!--/.nav-collapse -->
</div>
</nav>
<body id="app">
<div class="pageWrapper">
<div id="gisMap"></div>
<div id="content"></div>
</div>
<script src="/js/bundle.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha256-KXn5puMvxCw+dAYznun+drMdG1IFl3agK0p/pqT9KAo= sha512-2e8qq0ETcfWRI4HJBzQiA3UoyFk6tbNyG+qSaIBZLyW9Xf3sWZHN/lxe9fTh1U45DpPf07yj94KsUHHWe4Yk1A==" crossorigin="anonymous"></script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBDucSpoWkWGH6n05GpjFLorktAzT1CuEc&callback=initMap" async defer></script>
</body>
</html>
SCSS
@import "node_modules/bootstrap-sass/assets/stylesheets/bootstrap";
@import "partials/forms";
html, body {
height: 100%;
padding-top: 25px;
}
.pageWrapper {
background-color: red;
height:100%;
width: 100%;
}
#gisMap {
height: 100%;
width: 50%;
background-color: green;
}
最佳答案
确保 initMap 函数在全局范围 中可见,或者作为回调传递给 google maps.js 的参数是正确命名空间。 对于您的情况,最快的解决方案是更换:
function initMap(){
//..
}
到:
window.initMap = function(){
//...
}
或命名空间版本:
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBDucSpoWkWGH6n05GpjFLorktAzT1CuEc&callback=YOUR.NAMESPACE.initMap" async defer></script>
//编辑:
我看到在您的代码片段中您使用了一些异步模块加载(require.js?)并且您创建 window.initMap 函数的代码不会被执行,除非您调用包含此声明的模块。因此,您还没有满足我提到的第一个条件 - 在调用 google maps.js 之前,initMap 必须在全局范围内可见。
关于javascript - Google map 不会在页面加载时加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34466718/